队列消费者
@Service public class JmsConsumer { @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-activemq.xml"); JmsConsumer consumer = (JmsConsumer) ioc.getBean("jmsConsumer"); String value = (String) consumer.jmsTemplate.receiveAndConvert(); System.out.println(value); } }主题生产者和消费者
只需要修改配置文件目的地即可
<!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 --> <bean> <!-- 传入连接工厂 --> <property ref="connectionFactory"/> <!-- 传入目的地 --> <property ref="destinationTopic"/> <!-- 消息自动转换器 --> <property> <bean/> </property> </bean>配置消费者的监听类
写了一个类来实现消息监听后,只需要启动生产者,消费者不需要启动就自动会监听记录!
<!-- 配置监听程序 --> <bean> <property ref="connectionFactory"/> <property ref="destinationTopic"/> </bean> <bean> </bean> @Component public class MyMessageListener implements MessageListener { @Override public void onMessage(Message message) { if(null != message && message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; try { System.out.println(textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } } } 6. SpringBoot整合ActiveMQ个人不太赞成使用这种方式SpringBoot整合ActiveMQ,因为这样做会失去原生代码的部分功能和灵活性。但是工作中这种方式做能够满足我们常见的需求,也方便和简化我们的代码,也为了适应工作中大家的习惯。
6.1 队列案例 - 生产者点击投递pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> </parent> <groupId>com.polaris</groupId> <artifactId>springboot-activemq</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> <version>2.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>application.yml
# web占用的端口 server: port: 8085 spring: activemq: # activemq的broker的url broker-url: tcp://mpolaris.top:61616 # 连接activemq的broker所需的账号和密码 user: admin password: admin jms: # 目的地是queue还是topic, false(默认)=queue true=topic pub-sub-domain: false # 自定义队列名称,这只是个常量 myqueue: boot-activemq-queueActiveMQ配置类
@Configuration @EnableJms //开启Jms适配的注解 public class ConfigBean { @Value("${myqueue}") private String myQueue; //注入目的地 @Bean public Queue queue() { return new ActiveMQQueue(myQueue); } }队列消息生产者
@Component public class QueueProduce { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; public void produceMsg() { jmsMessagingTemplate.convertAndSend(queue,"===> SpringBoot + ActiveMQ消息"); } }