springboot整合ActiveMQ(点对点+发布订阅)

简介: springboot整合ActiveMQ(点对点+发布订阅)

生产者项目


<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
    </dependencies>
application.yml
spring:
  activemq:
    #Mq连接通讯地址
    broker-url: tcp://127.0.0.1:61616
    #账号
    user: admin
    #密码
    password: admin
#自定义队列名称
my_queue: springboot-queue
server:
  port: 8080
config类
package com.vhukze.config;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
import javax.jms.Topic;
@Component
public class ConfigQueue {
    @Value("${my_queue}")
    private String myQueue;
    //首先将队列注入到springboot容器中
    @Bean
    public Queue queue(){
        return new ActiveMQQueue(myQueue);
    }
//    //首先将主题注入到springboot容器中
//    @Bean
//    public Topic topic(){
//        return new ActiveMQTopic(myQueue);
//    }
}


生产者类

package com.vhukze.producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
import javax.jms.Topic;
@Component
public class P2pProducer {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Queue queue;
//    @Autowired
//    private Topic topic;
    //每隔五秒向队列发送消息
    @Scheduled(fixedDelay = 5000)
    public void send(){
        String msg = System.currentTimeMillis() + "";
        jmsMessagingTemplate.convertAndSend(queue,msg);
//        jmsMessagingTemplate.convertAndSend(topic,str);
        System.out.println("采用点对点模式发送消息"+msg);
    }
}


注意:在启动类添加开启定时任务


@EnableScheduling


消费者


依赖依然是上面的那些


配置文件把端口号改了,8080生产者用了


消费者类


package com.vhukze.consumer;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
public class P2pConsumer {
    //使用此注解即可监听指定名称的队列
    @JmsListener(destination = "${my_queue}")
    public void recive(String msg){
        System.out.println("采用点对点模式消费者成功获取到生产者的消息:"+msg);
    }
}

这是点对点模式,发布订阅模式只需要把代码中注释打开,换了Queue的即可


然后在配置文件中添加开启发布订阅模式




 

相关文章
|
4月前
|
消息中间件 Java
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
14 0
|
4月前
|
消息中间件 Java Kafka
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
31 0
|
10月前
|
消息中间件 Java
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
SpringBoot使用ActiveMq同时支持点对点推送和发布订阅
81 0
|
10月前
|
消息中间件 Java Kafka
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
SpringBoot整合 ActiveMQ快速入门 实现点对点推送
127 0
|
消息中间件 网络协议 Java
springboot整合jms之activemq
springboot整合jms之activemq
130 0
springboot整合jms之activemq
|
消息中间件 存储 安全
干货|SpringBoot JMS(ActiveMQ)API实践应用详解
Active是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的、可扩展的、稳定的和安全的企业级消息通信。AC-tiveMQ使用Apache提供的...
254 0
|
消息中间件 Java 网络协议
SpringBoot 整合 JMS(ActiveMQ)
SpringBoot整合JMS(ActiveMQ) 消息队列(Message Queue)是一种进程间或线程间的异步通信方式,使用消息队列消息生产者在消息产生后,会将消息保存在消息队列中直到消费者来取走它,即消息的发送者和接收者不需要与消息队列交互。
1340 0
|
消息中间件 Java 网络协议
SpringBoot整合ActiveMQ实现Email发送:专业负责MQ20年~
javax.mail 依赖__javax.mail + mq javax.mail mail 1.
2522 0
|
消息中间件 Java Apache
SpringBoot整合ActiveMQ:专业负责MQ20年~
ACTIVEMQ ActiveMQ 下载MQ后启动一下(Windows版本) #启动信息 ... jvm 1 | INFO | Listening for connections at ws://JackWen:61614?maximumConnections=1000&wireFormat.
1457 0
|
消息中间件 缓存 安全
SpringBoot与JMS集成(中间件为ActiveMQ)
Apache ActiveMQ是最受欢迎和强有力的开源消息和集成模式服务器,支持许多跨语言客户端和协议,便利使用企业集成模式还有许多先进的特性。