Introduction to Message Queuing

1.activetyMQ

1. Download ActiveMQ 
and go to the official website to download: http://activemq.apache.org/ 

2. Run ActiveMQ 
to decompress apache-activemq-5.8.0-bin.zip, and then double-click apache-activemq-5.5.1\bin\activemq.bat to run the ActiveMQ program. 

After starting ActiveMQ, log in: http://localhost:8161/admin/, create a Queue and name it FirstQueue. 

producer

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSProducer {

    
    public  static  void main(String[] args) {
         try {
             // The first step: create a ConnectionFactory factory object, you need to fill in the user name, password, and address to connect, all use the default, the default port is "tcp: // localhost:61616" 
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                    ActiveMQConnectionFactory.DEFAULT_USER,
                    ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                    "failover:(tcp://localhost:61616)?Randomize=false");
            
            // Step 2: We create a Connection connection through the ConnectionFactory factory object, and call the start method of Connection to open the connection. Connection is closed by default. 
            Connection connection = connectionFactory.createConnection();
            connection.start();
            
            // Step 3: Create a Session session (context object) through the Connection object to receive messages. Parameter configuration 1 is whether to enable a transaction, and parameter configuration 2 is the sign-in mode. Generally, we set up automatic sign-in. 
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            
            // Step 4: Create a Destination object through Session, which refers to an object used by a client to specify the target of producing messages and the source of consuming messages. In PTP mode, Destination is called Queue or queue; in Pub/Sub mode, Destination is called Topic or topic. Multiple Queues and Topics can be used in a program. 
            Destination destination = session.createQueue("HelloWorld" );
            
            // Step 5: We need to create message sending and receiving objects (producers and consumers) MessageProducer/MessageConsumer through the Session object. 
            MessageProducer producer = session.createProducer( null );
            
            // Step 6: We can use the setDeliveryMode method of MessageProducer to set persistent and non-persistent features (DeliveryMode) for it, which we will introduce in detail later.
            // producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            
            // Step 7: Finally, we use the JMS standard TextMessage form to create the data (through the Session object), and send the data with the MessageProducer's send method. Similarly, the client uses the receive method to receive data. Finally don't forget to close the Connection connection.
            
            for(int i = 0 ; i < 10 ; i ++){
                TextMessage msg = session.createTextMessage("I am the message content" + i);
                 // The first parameter target address
                 // The second parameter specific data information
                 // The third parameter transmits the data mode
                 // The fourth Parameter priority
                 // The expiration time of the fifth parameter message 
                producer.send(destination, msg, DeliveryMode.NON_PERSISTENT, 0 , 100000L );
                System.out.println( "Send message: " + msg.getText());
                session.commit(); // Remember to commit the transaction when enabling the transaction, otherwise the consumer will not receive the message 
                Thread.sleep(1000 );
            }

            if(connection != null){
                connection.close();
            }            
        } catch (Exception e) {
            e.printStackTrace ();
        }
        
    }

}

consumer

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * The consumer (receiver) of the message
 *
 * @author Administrator
 *
 */ 
public  class JMSConsumer {


    public  static  void main(String[] args) {
         try {
             // The first step: create a ConnectionFactory factory object, you need to fill in the user name, password, and address to connect, all use the default, the default port is "tcp: // localhost:61616" 
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                    ActiveMQConnectionFactory.DEFAULT_USER,
                    ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                    "failover:(tcp://localhost:61616)?Randomize=false");
            
            // Step 2: We create a Connection connection through the ConnectionFactory factory object, and call the start method of Connection to open the connection. Connection is closed by default. 
            Connection connection = connectionFactory.createConnection();
            connection.start();
            
            // Step 3: Create a Session session (context object) through the Connection object to receive messages. Parameter configuration 1 is whether to enable a transaction, and parameter configuration 2 is the sign-in mode. Generally, we set up automatic sign-in. 
            Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            
            // Step 4: Create a Destination object through Session, which refers to an object used by a client to specify the target of producing messages and the source of consuming messages. In PTP mode, Destination is called Queue or queue; in Pub/Sub mode, Destination is called Topic or topic. Multiple Queues and Topics can be used in a program. 
            Destination destination = session.createQueue("HelloWorld" );
             // Step 5: Create MessageConsumer through Session 
            MessageConsumer consumer = session.createConsumer(destination);
            
            while(true){
                TextMessage msg = (TextMessage)consumer.receive();
                if(msg == null) {
                    break;
                }
                System.out.println( "Received content: " + msg.getText());
            }            
        } catch (Exception e) {
            e.printStackTrace ();
        }
        
    }

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325318944&siteId=291194637