SpringBoot--整合ActiveMQ

1. 创建项目,添加ActiveMQ依赖
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
2. 在application.properties中进行连接配置
spring.activemq.broker-url=tcp://localhost:61616 #端口
spring.activemq.packages.trust-all=true #信任所有的包
spring.activemq.user=admin #用户名
spring.activemq.password=admin #密码
编码

提供一个消息队列Bean,该Bean的实例就由ActiveMQ提供:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    Queue queue(){
        return new ActiveMQQueue("amq");
    }

}

创建一个JMS组件来完成消息的发送和接收:

@Component
public class JmsComponent {

    @Autowired
    JmsMessagingTemplate messagingTemplate;

    @Autowired
    Queue queue;

    public void send(Message msg){
        messagingTemplate.convertAndSend(this.queue, msg);
    }

    @JmsListener(destination = "amq")
    public void receive(Message msg){
        System.out.println("receive:" + msg);
    }
}
4. 测试
	@Autowired
    JmsComponent jmsComponent;

    @Test
    public void test(){
        Message msg = new Message();
        msg.setContent("hello jms!");
        msg.setDate(new Date());
        jmsComponent.send(msg);
    }

控制台打印:

receive:com.example.demo.pojo.Message@78a8611e

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值