[MQ]消息队列整合

Spring整合ActiveMQ

Springboot整合ActiveMQ

queue生产者

新建项目

pom.xml

				<parent>
		      <groupId>org.springframework.boot</groupId>
		      <artifactId>spring-boot-starter-parent</artifactId>
		      <version>2.1.5.RELEASE</version>
		      <relativePath/> <!-- lookup parent from repository -->
		   </parent>
		   <groupId>com.at.boot.activemq</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>
		
		   <build>
		      <plugins>
		         <plugin>
		            <groupId>org.springframework.boot</groupId>
		            <artifactId>spring-boot-maven-plugin</artifactId>
		         </plugin>
		      </plugins>
		   </build>
		</project>

application.yml

		# web占用的端口
		server:
		  port: 7777
		
		spring:
		  activemq:
		    # activemq的broker的url,指定连接哪一台MQ
		    broker-url: tcp://192.168.17.3:61616
		    # 连接activemq的broker所需的账号和密码
		    user: admin
		    password: admin
		  jms:
		    # 目的地是queue还是topic
		    pub-sub-domain: false  // false(默认) = queue    true =  topic
			#自定义队列名称。这只是个常量
		myqueue: boot-activemq-queue

配置目的地的bean

配置目的地的bean和开启springboot的jms功能

		package com.at.boot.activemq.config;
		
		import org.apache.activemq.command.ActiveMQQueue;
		import org.springframework.beans.factory.annotation.Value;
		import org.springframework.context.annotation.Bean;
		import org.springframework.jms.annotation.EnableJms;
		import org.springframework.stereotype.Component;
		
		// 让spring管理的注解,相当于spring中在xml 中写了个bean
		@Component
		// 开启jms适配
		@EnableJms
		public class ConfigBean {
		
		        //通过@value注解, 注入配置文件中的 myqueue
		        @Value("${myqueue}")
		        private String myQueue ;
		
		        @Bean   // bean id=""  class="…"
		        public ActiveMQQueue queue(){
		             return  new ActiveMQQueue(myQueue);
		        }
		}

队列生产者代码

相当于service层
package com.at.boot.activemq.produce;

				import org.springframework.beans.factory.annotation.Autowired;
				import org.springframework.jms.core.JmsMessagingTemplate;
				import org.springframework.scheduling.annotation.Scheduled;
				import org.springframework.stereotype.Component;
				import javax.jms.Queue;
				import java.util.UUID;
				
				@Component
				public class Queue_Produce {
				
				    // JMS模板
				    @Autowired
				    private JmsMessagingTemplate  jmsMessagingTemplate ;
				
				    // 这个是我们配置的队列目的地
				    @Autowired
				    private Queue queue ;
				
				    // 发送消息
				    public void produceMessage(){
				        // 一参是目的地,二参是消息的内容
				        jmsMessagingTemplate.convertAndSend(queue,"****"+ UUID.randomUUID().toString().substring(0,6));
				    }
				
				    // 定时任务。每3秒执行一次。非必须代码,仅为演示。
				    @Scheduled(fixedDelay = 3000)
				    public void produceMessageScheduled(){
				        produceMessage();
				    }
				}
  • 触发投递和定时投递必须掌握

主启动类(非必须,仅为演示)

			package com.at.boot.activemq;
			
			import org.springframework.boot.SpringApplication;
			import org.springframework.boot.autoconfigure.SpringBootApplication;
			import org.springframework.scheduling.annotation.EnableScheduling;
			
			@SpringBootApplication
			// 是否开启定时任务调度功能
			@EnableScheduling
			public class MainApp_Produce {
			    public static void main(String[] args) {
			        SpringApplication.run(MainApp_Produce.class,args);
			    }
			}

单元测试(非必须,仅为演示)

			package com.at.boot.activemq;
			
			import com.at.boot.activemq.produce.Queue_Produce;
			import org.junit.Test;
			import org.junit.runner.RunWith;
			import org.springframework.boot.test.context.SpringBootTest;
			import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
			import org.springframework.test.context.web.WebAppConfiguration;
			import javax.annotation.Resource;
			
			// 加载主类
			@SpringBootTest(classes = MainApp_Produce.class)
			// 加载spring的junit
			@RunWith(SpringJUnit4ClassRunner.class)
			// 加载web
			@WebAppConfiguration
			public class TestActiveMQ {
			
			    @Resource    //  这个是java 的注解,而Autowried 是 spring 的
			    private Queue_Produce  queue_produce ;
			
			//  这个是java 的注解,而Autowried 是 spring 的
			    @Test
			    public  void testSend() throws Exception{
			        queue_produce.produceMessage();
			    }
			}

queue消费者

pom.xml

application.yml

代码

注册一个消息监听器。项目开启后监听某个主题的消息

			package com.at.boot.activemq.consummer;
	
	import org.springframework.jms.annotation.JmsListener;
	import org.springframework.stereotype.Component;
	import javax.jms.TextMessage;
	
	@Component
	public class Queue_consummer {
	
	    // 注册一个监听器。destination指定监听的主题。
	    @JmsListener(destination = "${myqueue}")
	    public void receive(TextMessage textMessage) throws  Exception{
	        System.out.println(" ***  消费者收到消息  ***"+textMessage.getText());
	    }
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值