SpringBoot 整合 ActiveMQ Demo运用

启动mq 服务 访问http://localhost:8161/admin

建一个配置类

@Configuration
@EnableJms
public class ActiveMQConfig {
    @Bean//(name = "newFileQueue") 
    public Queue queue() {
        return new ActiveMQQueue("zskk.new.queue");
    }
    @Bean
    public Topic topic() {
        return new ActiveMQTopic("zskk.new.topic");
    }
}

生产者

@Component
@EnableScheduling
public class QueueProducer {  
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Queue queue;
    @Scheduled(fixedDelay=3000)//每3s执行1次
    public void send() {
       try {
		
		   MapMessage mapMessage = new ActiveMQMapMessage();
		   mapMessage.setString("info", "你还在睡觉");
		   
		   this.jmsMessagingTemplate.convertAndSend(this.queue, mapMessage);
		   
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}  

消费

@Component
public class QueueConsumer {  
    //使用JmsListener配置消费者监听的队列,其中Message是接收到的消息  
	@JmsListener(destination = "czxy.queue")
    public void receiveQueue(Message message) {
		try {
			MapMessage mapMessage = (MapMessage) message;
			String info = mapMessage.getString("info");
			System.out.println(info+"1");
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}

你可能感兴趣的:(SpringBoot)