SpringBoot整合ActiveMQ

SpringBoot整合ActiveMQ

pom.xml添加的依赖如下

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

在application.properties中配置本地ActiveMQ

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.packages.trust-all=true
spring.activemq.user=admin
spring.activemq.password=admin

在启动类父文件夹下新建Message.java

public class Message implements Serializable {
    private String content;
    private Date sendDate;

    @Override
    public String toString() {
        return "Message{" +
                "content='" + content + '\'' +
                ", sendDate=" + sendDate +
                '}';
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getSendDate() {
        return sendDate;
    }

    public void setSendDate(Date sendDate) {
        this.sendDate = sendDate;
    }
}

在启动类父文件夹下新建JmsComponent.java

@Component
public class JmsComponent {
    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    Queue queue;

    public void send(Message message){
        jmsMessagingTemplate.convertAndSend(this.queue,message);
    }
    @JmsListener(destination = "hello.xiaoliu")
    public void receive(Message message){
        System.out.println(message);
    }
}

在启动类中加入以下代码

@SpringBootApplication
public class ActivemqApplication {

    public static void main(String[] args) {
        SpringApplication.run(ActivemqApplication.class, args);
    }
    @Bean
    Queue queue(){
        return new ActiveMQQueue("hello.xiaoliu");
    }
}

在测试类中进行测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivemqApplicationTests {

    @Autowired
    JmsComponent jmsComponent;
    @Test
    public void contextLoads() {
        Message message=new Message();
        message.setContent("hello xiaoliu!");
        message.setSendDate(new Date());
        jmsComponent.send(message);
    }
}

启动本地AcitiveMQ并且允许项目

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xAWGKDzu-1588335766599)(C:\Users\MI\AppData\Roaming\Typora\typora-user-images\1588335758412.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值