ActiveMQ-- mode (Queue Mode / themes)

Two modes: queue mode / Themes

pom.xml

<dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.15.9</version>
</dependency>  

 

Queue mode, in fact, eating patterns.

   Such as the production side sent a message to activeMQ server 10, but this time with a plurality of consumer, then the consumer will divide these 10 messages, a message is only to obtain a consumer.
Theme is subscription model.

  For example, producers made 10 messages at a time when there are more than a consumer, then the consumer can get more than 10 this news, just like the public subscription number so.


 

Queue Mode:

1. First run twice TestConsumer class to start two different consumers
2. Run once TestProducer, to start the producer

  Producers to produce 100, two consumer divide

  consumer:

public  class TestConsumer {
     // service address, the default port 61616 
    Private  static  Final String url = "tcp: //127.0.0.1: 61616" ;
     // The name of the message consumption 
    Private  static  Final String topicName = "queue_style" ; 

    // consumers may be more, in order to distinguish between different consumers, to create a random name 
    Private  static  Final String consumerName = "consumer-" + RandomUtil.randomString (5 );
     public  static  void main (String [] args) throws JMSException {
         // 0.5 port to determine whether to activate the Active MQ server
         ActiveMQUtil.checkServer ();
        System.out.printf ("% s consumer launch of the n-%." , consumerName); 

        // 1. create ConnectiongFactory, binding address 
        ConnectionFactory = Factory's new new ActiveMQConnectionFactory (url);
         // 2. Create a Connection 
        Connection Connection = factory.createConnection ();
         / / 3. start connection 
        connection.start ();
         // 4. Create a session 
        the session = connection.createSession the session ( to false , Session.AUTO_ACKNOWLEDGE);
         // 5. The Create an object (subject type) 
        the Destination Where do you want = session.createQueue (topicName );
         // 6. create a consumer 
        MessageConsumer consumer =session.createConsumer(destination);
        //7.创建一个监听器
        consumer.setMessageListener(new MessageListener() {

            public void onMessage(Message arg0) {
                // TODO Auto-generated method stub
                TextMessage textMessage=(TextMessage)arg0;
                try {
                    System.out.println(consumerName +" 接收消息:"+textMessage.getText());
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace ();
                }

            } 
        }); 
        
        // 8 because they do not know when, so I can not take the initiative to close, not closed, has been in a listening state
         // Connection.close (); 
    } 
}

Producer:

public  class TestProducer { 

    // service address, the default port 61616 
    Private  static  Final String url = "tcp: //127.0.0.1: 61616" ;
     // name of the message sent by the 
    Private  static  Final String topicName = "queue_style" ;
     public  static  void main (String [] args) throws a JMSException {
         // of 0. the first port determines whether to activate the Active MQ server 
        ActiveMQUtil.checkServer ();
         // 1. Create ConnectiongFactory, binding address 
        the ConnectionFactory Factory = new new ActiveMQConnectionFactory (URL);
         / /2. Create Connection 
        Connection Connection = factory.createConnection ();
         // 3. Start connection 
        connection.start ();
         // 4. Create a session 
        the Session = connection.createSession the session ( to false , Session.AUTO_ACKNOWLEDGE);
         // 5. The Create a target (queue type) 
        the Destination Where do you want = session.createQueue (topicName);
         // 6. The producer creates a 
        the MessageProducer producer = session.createProducer (Where do you want); 


        for ( int I = 0; I <100; I ++ ) {
             // 7. create a message
            TextMessage textMessage = session.createTextMessage ( "queue message -" + I);
             // 8. The send message 
            producer.send (textMessage); 
            System.out.println ( "Send:" + textMessage.getText ()); 
        } 
        // 7. close the connection 
        Connection.close (); 
    } 
}

2 consumer:

 

 

Producers to produce:

 

 

 

 

 

 

 

 

 

 


 

Themes:

 

  Consumers, producers,

public class TestConsumer {
    //服务地址,端口默认61616
    private static final String url="tcp://127.0.0.1:61616";
    //这次消费的消息名称
    private static final String topicName="topic_style";

    //消费者有可能是多个,为了区分不同的消费者,为其创建随机名称
    private static final String consumerName="consumer-" + RandomUtil.randomString(5);
    public static void main(String[] args) throws JMSException {
        

        //0. 先判断端口是否启动了 Active MQ 服务器
        ActiveMQUtil.checkServer();
        System.out.printf("%s 消费者启动了。 %n", consumerName);
        //1.创建ConnectiongFactory,绑定地址
        ConnectionFactory factory=new ActiveMQConnectionFactory(url);
        //2.创建Connection
        Connection connection= factory.createConnection();
        //3.启动连接
        connection.start();
        //4.创建会话
        Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //5.创建一个目标 (主题类型)
        Destination destination=session.createTopic(topicName);

        //6.创建一个消费者
        MessageConsumer consumer=session.createConsumer(destination);
        //7.创建一个监听器
        consumer.setMessageListener(new MessageListener() {

            public void onMessage(Message arg0) {
                // TODO Auto-generated method stub
                TextMessage textMessage=(TextMessage)arg0;
                try {
                    System.out.println(consumerName +" 接收消息:"+textMessage.getText());
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });
        
        //8. 因为不知道什么时候有,所以没法主动关闭,就不关闭了,一直处于监听状态
        //connection.close();
    }
}
public class TestProducer {

    //服务地址,端口默认61616
    private static final String url="tcp://127.0.0.1:61616";
    //这次发送的消息名称
    private static final String topicName="topic_style";
    public static void main(String[] args) throws JMSException {
        //0. 先判断端口是否启动了  Active MQ 服务器
        ActiveMQUtil.checkServer();
        //1.创建ConnectiongFactory,绑定地址
        ConnectionFactory factory=new ActiveMQConnectionFactory(url);
        //2.创建Connection
        Connection connection= factory.createConnection();
        //3.启动连接
        connection.start();
        //4.创建会话
        Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5.创建一个目标 (主题类型)
        Destination destination=session.createTopic(topicName);
        //6.创建一个生产者
        MessageProducer producer=session.createProducer(destination);


        for (int i = 0; i < 100; i++) {
            //7.创建消息
            TextMessage textMessage=session.createTextMessage("主题消息-"+i);
            //8.发送消息
            producer.send(textMessage);
            System.out.println("发送:"+textMessage.getText());
        }
        //7. 关闭连接
        connection.close();
    }
}

 

生产者生产100个,两个消费者都分别接受了100个

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/crazy-lc/p/12243535.html