spring boot整合activemq

来源:互联网 发布:淘宝v6会员帐号值钱吗 编辑:程序博客网 时间:2024/06/06 20:36

1 activemq安装

1.1 下载压缩包

1.2 解压启动

tar -zxvf apache-activemq-5.14.0-bin.tar.gz

进去bin目录  cd apache-activemq-5.14.0/bin启动        ./activemq start

1.3 打开web管理页面

访问http://IP:8161/admin

启动后,activeMQ会占用两个端口,一个是负责接收发送消息的tcp端口:61616,一个是基于web负责用户界面化管理的端口:8161。这两个端口可以在conf下面的xml中找到。http服务器使用了jetty

2 项目集成activemq

2.1 配置文件设置application.properties

spring.activemq.broker-url=tcp://localhost:61616spring.activemq.user=adminspring.activemq.password=adminspring.activemq.in-memory=truespring.activemq.pool.enabled=false

2.2 生产者

@Componentpublic class Producer implements CommandLineRunner {    @Autowired    private JmsMessagingTemplate jmsMessagingTemplate;    @Autowired    private Queue queue;    @Override    public void run(String... args) throws Exception {        send("Sample message");        System.out.println("Message was sent to the Queue");    }    public void send(String msg) {        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);    }}

2.3 消费者

@Componentpublic class Consumer {    @JmsListener(destination = "sample.queue")    public void receiveQueue(String text) {        System.out.println(text);    }}

2.4 程序入口

@SpringBootApplication@EnableJmspublic class SampleActiveMQApplication {    @Bean    public Queue queue() {        return new ActiveMQQueue("sample.queue");    }    public static void main(String[] args) {        SpringApplication.run(SampleActiveMQApplication.class, args);    }}

代码有参考:github地址