1. 程式人生 > >SpringBoot+ActiveMQ多訊息佇列監聽

SpringBoot+ActiveMQ多訊息佇列監聽

之前專案都是用SpringMVC,後來整個專案開始用Springboot,果斷轉用Springboot,用了之後感覺根本停不下來。

其實springboot多訊息佇列監聽和spring基本一樣,只是配置上稍有區別'

1.在有@Configuration註解的java類中加如下配置

@Bean
	public DefaultMessageListenerContainer listenerContainer(){
		DefaultMessageListenerContainer m =new DefaultMessageListenerContainer();
		m.setConnectionFactory(connectionFactory);
		Destination d = new ActiveMQQueue("*");//*表示通配所有佇列名稱
		m.setDestination(d);
		m.setMessageListener(new QueueMessageListener());
		return m;		
	}

2.繼承MessageListener類
public class QueueMessageListener implements MessageListener {
	
	@Autowired
	private DataUploadService dataservice;

	@Override
	public void onMessage(Message message) {
		// TODO Auto-generated method stub
		dataservice = SpringUtils.getApplicationContext().getBean(DataUploadService.class);
		System.out.println("=================接收訊息=============="+message );
		//TextMessage tm = (TextMessage) message;//將訊息轉成text型別
		//下面就開始你的業務邏輯		
		
	}

}