- Spring整合ActiveMQ之队列生产消费者
- pom.xml所需的依赖
<dependencies> <!-- activemq 所需要的jar 包--> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.9</version> </dependency> <!-- activemq 和 spring 整合的基础包 --> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.1.RELEASE</version> </dependency> <!-- Spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.23.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.23.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.23.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.3.23.RELEASE</version> </dependency> <!-- activemq连接池 --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.10</version> </dependency> <!-- spring支持jms的包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- 嵌入式activemq的broker所需要的依赖包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.0</version> </dependency> </dependencies>
- 在resoueces文件夹下编写spring-activemq.xml文件
- 生产者代码
@Service public class SpringMQ_Consumer { @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("spring-activemq.xml"); SpringMQ_Consumer consumer=(SpringMQ_Consumer)ac.getBean("springMQ_Consumer"); String retValue=(String)consumer.jmsTemplate.receiveAndConvert(); System.out.println("****消费者收到的消息"+retValue); } }
- 消费者代码
@Service public class SpringMQ_Produce { @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("spring-activemq.xml"); SpringMQ_Produce produce=(SpringMQ_Produce)ac.getBean("springMQ_Produce"); /*produce.jmsTemplate.send(new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { TextMessage textMessage=(TextMessage) session.createTextMessage("****spring和ActiveMQ的整合*****"); return null; } });*/ produce.jmsTemplate.send((session)->{ TextMessage textMessage=(TextMessage) session.createTextMessage("****spring和ActiveMQ的整合*****"); return textMessage; }); System.out.println("send task over"); } }
- pom.xml所需的依赖
- Spring整合ActiveMQ之主题生产消费
- pom.xml文件之修改了目的地
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启包的自动扫描 --> <context:component-scan base-package="com.zb.activemq"></context:component-scan> <!-- 配置生产者 --> <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory"> <!-- 真正可以生产Connection的ConnectionFactory,由对应的JMS服务商提供 --> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.8.114:61616"></property> </bean> </property> <property name="maxConnections" value="100"></property> </bean> <!-- 这个是队列目的地,点对点的Queue --> <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> <!-- 通过构造注入Queue名 --> <constructor-arg index="0" value="spring-active-queue"></constructor-arg> </bean> <!-- 这个是队列目的地, 发布订阅的主题Topic--> <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg index="0" value="spring-active-topic"/> </bean> <!-- Spring提供的JMS工具类,他可以进行消息发送,接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!--传入连接工厂--> <property name="connectionFactory" ref="jmsFactory"/> <!-- 传入目的地--> <property name="defaultDestination" ref="destinationTopic"/> <!-- 消息自动转换器 --> <property name="messageConverter"> <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/> </property> </bean> </beans>
- 生产者代码:不需要改变代码
- 消费者代码:不需要改变代码
- pom.xml文件之修改了目的地
- Spring整合ActiveMQ之监听器配置
- 要求: 在spring里面实现消费者不启动,直接通过配置监听完成
- receive=>massageListener;不用先启动订阅才能收到消息
- 修改spring配置文件spring-activemq.xml
<!--配置监听程序 --> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsFactory"></property> <property name="destination" ref="destinationTopic"></property> <property name="messageListener" ref="myMessageListener"></property> </bean>
- MyMessageListener.java
- 只需要启动生产者,消费者不用启动,自动会监听记录
- SpringBoot整合ActiveMQ之队列生产者
- 队列
- 新建maven工程
- 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 http://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.1.5.RELEASE</version> <relativePath></relativePath> </parent> <groupId>com.zb</groupId> <artifactId>boot_mq_produce</artifactId> <version>1.0-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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--spring boot整合activemq的jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> <version>2.1.5.RELEASE</version> </dependency> </dependencies> </project>
- src/main/resources/application.yml
server: port: 7777 spring: activemq: broker-url: tcp://192.168.8.114:61616 #用自己的 user: admin password: admin jms: pub-sub-domain: false #false=Queue true=topic #自己定义队列名称 myqueue: boot-activemq-queue
- 配置bean(类似于Spring框架的applicationContext.xml文件)
@Component @EnableJms public class ConfigBean { @Value("${myqueue}") private String myQueue; @Bean //<bean id="" class=" "> public Queue queue(){ return new ActiveMQQueue(myQueue); } }
- Queue_Produce
@Component public class Queue_Produce { @Autowired private JmsMessagingTemplate template; @Autowired private Queue queue; public void productMsg(){ template.convertAndSend(queue,"***"+ UUID.randomUUID().toString().substring(0,6)); } @Scheduled(fixedDelay = 3000) //每隔多长时间自动调用我这个方法 public void productMsgScheduled(){ template.convertAndSend(queue,"***scheduled"+ UUID.randomUUID().toString().substring(0,6)); } }
- 主启动类MainApp_Produce
@SpringBootApplication // 是否开启定时任务调度功能 @EnableScheduling public class MainApp_Produce { public static void main(String[] args) { SpringApplication.run(MainApp_Produce.class,args); } }
- 测试单元
@SpringBootTest(classes = MainApp_Produce.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestActiveMQ { @Resource // 这个是java 的注解,而Autowried 是 spring 的 private Queue_Produce queue_produce ; @Test public void testSend() throws Exception{ queue_produce.productMsg(); } }
- 新需求,间隔3秒发送一条新消息
- 修改Queue_Produce新增定时投递
- 修改主启动类添加@EnableScheduling注解
- 直接启动主启动类,间隔发消息
- 队列
- SpringBoot整合ActiveMQ之队列消费者
- 新建maven工程
- pom.xml文件:和生产者的一样
- application.yml文件:和生产者的一样,只需要修改一下端口,每个微服务都占一个端口
- Queue_Consumer
@Component public class Queue_Consumer { @JmsListener(destination = "${myqueue}") public void receive(TextMessage textMessage)throws JMSException{ System.out.println("消费者收到消息:"+textMessage.getText()); } }
- 主启动类
@SpringBootApplication public class MainApp_Consumer { public static void main(String[] args) { SpringApplication.run(MainApp_Consumer.class,args); } }
- SpringBoot整合ActiveMQ之主题生产者
- 新建maven项目
- pom.xml:和上面的一样
- application.yml
server: port: 6666 spring: activemq: broker-url: tcp://192.168.8.114:61616 #用自己的 user: admin password: admin jms: pub-sub-domain: true #false=Queue true=topic #自己定义主题名称 myTopic: boot-activemq-topic
- 配置Bean
@Component public class ConfigBean { @Value("${myTopic}") private String topicName; @Bean public Topic topic(){ return new ActiveMQTopic(topicName); } }
- Topic_Produce
@Component public class Topic_Produce { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Topic topic; @Scheduled(fixedDelay = 3000) public void produceTopic(){ jmsMessagingTemplate.convertAndSend(topic,"主题消息:"+ UUID.randomUUID().toString().substring(0,6)); } }
- 主启动类MainApp_TopicProduce
@SpringBootApplication @EnableScheduling public class MainApp_TopicProduce { public static void main(String[] args) { SpringApplication.run(MainApp_TopicProduce.class,args); } }
- SpringBoot整合ActiveMQ之主题消费者
- 新建maven
- pom.xml
- application.yml
server: port: 5566 spring: activemq: broker-url: tcp://192.168.8.114:61616 #用自己的 user: admin password: admin jms: pub-sub-domain: true #false=Queue true=topic #自己定义主题名称 myTopic: boot-activemq-topic
- Topic_Consumer
@Component public class Topic_Consumer { @JmsListener(destination = "${myTopic}") public void receive(TextMessage text)throws JMSException { try{ System.out.println("消费者收到订阅:"+text.getText()); }catch (Exception e){ e.printStackTrace(); } } }
- 模拟有两个订阅者主启动类,先启动订阅者在启动消费者
- MainApp5555
@SpringBootApplication public class MainApp5555 { public static void main(String[] args) { SpringApplication.run(MainApp5555.class,args); } }
- MainApp5566
@SpringBootApplication public class MainApp5566 { public static void main(String[] args) { SpringApplication.run(MainApp5566.class,args); } }
ActiveMQ(消息中间件)学习笔记 (三)
最新推荐文章于 2021-10-15 17:04:29 发布