And transmitting the transmission target string ActiveMQ

Import ActiveMQ at the beginning of the project lead pack

The first to introduce the ActiveMQ time (the message queue)

* Message Queue
JMS, Java-defined set of interfaces, ActiveMQ JMS to achieve the

High concurrency processing / decoupling applications

There are two types of queues:
1.Queue
net Queue in the news release, while consumption only by a client
if the client is not yo, then the message has been stored in the Queue
until a client to consume
2, Topic
to Topic in the news release, will also be on multiple clients consumption (1 to many)
If the message queue
JMS, Java-defined set of interfaces, ActiveMQ JMS to achieve the

High concurrency processing / decoupling applications

There are two types of queues:
1.Queue
net Queue in the news release, while consumption only by a client
if the client is not yo, then the message has been stored in the Queue
until a client to consume
2, Topic
to Topic in the news release, will also be on multiple clients consumption (1 to many)
If the message queue
JMS, Java-defined set of interfaces, ActiveMQ JMS to achieve the

High concurrency processing / decoupling applications

There are two types of queues:
1.Queue
net Queue in the news release, while consumption only by a client
if the client is not yo, then the message has been stored in the Queue
until a client to consume.
2, Topic
to Topic in place of the message, and it will be for multiple clients consumption (1 to many)
if there is no client, then this message will be lost.

It is being used in mirroring configured ActiveMQ

Configured port mapping user name and password in the configuration file

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin

#spring.activemq.packages.trust-all=false
# 他的默认值是false 只支持Queue
#改为true 支持 topic
#所以不能再配置文件中设置
#需要配置bean

Creating JmsConfig entity class to write about all configured message queue in this entity class


@Configuration
public class JmsConfig {

    //    编辑字符串Queue 消息对列的名字
    public static final String Quene_SEND_EMAIL = "com.lanou.QUEUE_SEND_EMAIL";
    //   编辑字符串Topic 消息对列的名字
    public static final String TOPIC_USER = "com.lanou.TOPIC_USER";
    //    编辑对象Queue 消息对列的名字
    public static final String QUEUE_EMAIL = "com.lanou.QUEUE_EMAIL";


    //    设置字符串Queue 形式的方法
    @Bean
    public Queue emailQueue() {
        return new ActiveMQQueue(Quene_SEND_EMAIL);
    }

    //  发送字符串Topic 形式的方法
    @Bean
    public Topic userTopic() {
        return new ActiveMQTopic(TOPIC_USER);
    }

    //    发送对象Queue 形式的方法
    @Bean
    public Queue messageQueue() {
        return new ActiveMQQueue(QUEUE_EMAIL);
    }


    @Bean
    //    创建工厂(Queue 对列形式)
    public DefaultJmsListenerContainerFactory queueFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory
                = new DefaultJmsListenerContainerFactory();

        factory.setPubSubDomain(false);
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

    @Bean
    //    创建工厂(Topic 队列形式)
    public DefaultJmsListenerContainerFactory topicFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory
                = new DefaultJmsListenerContainerFactory();

        factory.setPubSubDomain(true);
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

Introducing layer disposed on a good service to send information to the message queue Bean


@Service
public class UserService {

    //    引入Queue 形式发送字符串的方法
    @Resource
    private Queue emailQueue;
    //    引入Topic 形式发送字符串的方法
    @Resource
    private Topic userTopic;
    //    引入Queue 形式发送对象的方法
    @Resource
    private Queue messageQueue;
    //    引入实体类JmsMessagingTemplate
    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;


    //    发送字符串到Queue 形式的消息队列
    public void register(String username) {
        //1.把用户信息插入到数据库
        System.out.println("插入数据库" + username);
        //2.把用户信息放到消息队列中
        // 第一个参数 消息对例的名字 ,  GenericMessage<> 形式的字符串
        Message<String> message = new GenericMessage<>(username);
        this.jmsMessagingTemplate.send(emailQueue, message);

    }

    //    发送字符串到Topic 形式的消息队列
    public void login(String username) {
        // 第一个参数 消息对例的名字 ,  GenericMessage<> 形式的字符串
        Message<String> message = new GenericMessage<>(username);
        this.jmsMessagingTemplate.send(userTopic, message);
    }


    //    发送对象到Queue 形式的消息对列
    public void send(User user) {
        this.jmsMessagingTemplate.convertAndSend(messageQueue, user);

    }
}

EmailConsumer to create entity classes inside consumer information


@Component
public class EmailConsumer {

    //  @JmsListener(  参数一 对列的格式  ,   参数二  与配置文件方法名相对应)

    //    用来消费Queue 消息
    @JmsListener(destination = JmsConfig.Quene_SEND_EMAIL, containerFactory = "queueFactory")
    public void receive(String message) {
        //执行真正发送邮件的功能
        System.out.println(message);
    }

    //    用来消费Topic1 消息
    @JmsListener(destination = JmsConfig.TOPIC_USER, containerFactory = "topicFactory")
    public void receiveTopic1(String message) {
        //执行真正发送邮件的功能
        System.out.println("客户端1" + message);
    }

    @JmsListener(destination = JmsConfig.TOPIC_USER, containerFactory = "topicFactory")
    public void receiveTopic2(String message) {
        //执行真正发送邮件的功能
        System.out.println("客户端2" + message);
    }

    @JmsListener(destination = JmsConfig.TOPIC_USER, containerFactory = "topicFactory")
    public void receiveTopic3(String message) {
        //执行真正发送邮件的功能
        System.out.println("客户端3" + message);
    }

    //    接收消费对象
    @JmsListener(destination = JmsConfig.QUEUE_EMAIL, containerFactory = "queueFactory")
    public void sends(User message) {
        System.out.println("回掉成功了");
        System.out.println("收件人" + message.getTo());
        System.out.println("标题" + message.getTitle());
        System.out.println("内容" + message.getText());
     }
}

Guess you like

Origin www.cnblogs.com/lxx-1843693653/p/11130613.html