JMS-ActiveMq- ad-hoc mode

Point (Point-to-Point). In the point to point messaging system, a separate messages to the user. Message queue often point (javax.jms.Queue) is associated.

There are two forms of consumption, we only introduce the listener mode (Code lengthy, late util-yourself package)

  • Real-time monitoring, producers of consumer monitor production status
  • Recive timed refresh mode (not practical, not recommended

Version: apache-activemq-5.11.1 official website to download

Home Page: http: //activemq.apache.org/
latest version: 5.11.1
SDK and source code Download: http: //activemq.apache.org/activemq-5111-release.html
ActiveMQ service starts Address: http: //127.0.0.1:8161/admin/ username / password admin / admin

1. Producers JMSProducer

/ ** 
 * message producer 
 * @author Administrator 
 * 
 * / 
public  class the JMSProducer { 

    Private  static  Final String USERNAME = ActiveMQConnection.DEFAULT_USER; // default connection username 
    Private  static  Final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; // default connection password 
    Private  static  Final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL; // default connection address 
    Private  static  Final  int SENDNUM = 10; // number of messages sent by 
    
    public  static void main (String [] args) { 
        
        the ConnectionFactory The connectionFactory; // factory connected 
        Connection Connection = null ; // connect 
        the Session the session; // session sent or received message thread 
        the Destination Where do you want; // destination of the message 
        the MessageProducer MessageProducer; // A message producer 
        
        // instantiate the connection factory 
        The connectionFactory = new new ActiveMQConnectionFactory (JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL); 
        
        the try { 
            connection = connectionFactory.createConnection (); //Obtaining a connection via the connection factory 
            connection.start (); // start connecting 
            the session = connection.createSession (of Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // Create the Session 
            Where do you want = session.createQueue ( "FirstQueue1"); // Create message queue 
            = session.createProducer MessageProducer (Where do you want); // Create a message producer 
            the sendMessage (the session, MessageProducer); // send message 
            Session.commit (); 
        } the catch (Exception E) {
             // the TODO Auto-Generated Block the catch 
            e.printStackTrace (); 
        } the finally{
            if(connection!=null){
                try {
                    connection.close();
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 发送消息
     * @param session
     * @param messageProducer
     * @throws Exception
     */
    public static voidthe sendMessage (the Session the session, the MessageProducer MessageProducer) throws Exception {
         for ( int I = 0; I <JMSProducer.SENDNUM; I ++ ) { 
            TextMessage Message = session.createTextMessage ( "message transmitted ActiveMQ" + I); 
            System.out.println ( "send message:" + "message transmitted ActiveMQ" + I); 
            messageProducer.send (message); 
        } 
    } 
}

Write 2.listener

/**
 * 消息监听
 * @author Administrator
 *
 */
public class Listener implements MessageListener{

    @Override
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
        try {
            System.out.println("收到的消息:"+((TextMessage)message).getText());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

3. Consumers

/ ** 
 * message consumer 
 * @author Administrator 
 * 
 * / 
public  class JMSConsumer2 { 

    Private  static  Final String USERNAME = ActiveMQConnection.DEFAULT_USER; // default connection username 
    Private  static  Final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; // default connection password 
    Private  static  Final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL; // default connection address 
    
    public  static  void main (String [] args) { 
        the ConnectionFactory the connectionFactory; // connection factory
        Connection = Connection null ; // connect 
        the Session the session; // session sent or received message thread 
        the Destination Where do you want; // destination of the message 
        a MessageConsumer MessageConsumer; // message consumer 
        
        // instantiate the connection factory 
        The connectionFactory = new new ActiveMQConnectionFactory (JMSConsumer2 .USERNAME, JMSConsumer2.PASSWORD, JMSConsumer2.BROKEURL); 
                
        the try { 
            connection = connectionFactory.createConnection ();   // get a connection via the connection factory 
            connection.start (); // start connection 
        // consumers need to open a transaction parameters : whether to open the transaction parameters II: consumer type
= connection.createSession the session (Boolean.FALSE The, Session.AUTO_ACKNOWLEDGE); // Create the Session Where do you want = session.createQueue ( "FirstQueue1"); // create a connection message queue MessageConsumer = session.createConsumer (Where do you want); // Create message consumer messageConsumer.setMessageListener ( new new The listener ()); // registered message listener } the catch (a JMSException E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } } }

4.session way of explanation

Session.AUTO_ACKNOWLEDGE. When customers receive a successful return from the method, or from MessageListener.onMessage 
when the successful return of the method, the session automatically acknowledges a message that the customer receives. 
Session.CLIENT_ACKNOWLEDGE. Customer acknowledges acknowledge the message by the method of the message. Note that, in this mode 
the formula, is confirmed at the session layer: a confirmation message will automatically be consumed confirm all message session has been consumed. For example, if Yi 
message consumer spending by 10 news, then confirm the first five messages, then all 10 messages have been confirmed. 
Session.DUPS_ACKNOWLEDGE. This selection is just dull session acknowledgment message submitted. If the JMS provider fails, you may 
be able to cause some duplicate messages. If the message is a duplicate, then the JMS provider must JMSRedelivered field of the header is set 
to true.

5. Test the main page

 

Guess you like

Origin www.cnblogs.com/cbpm-wuhq/p/11942849.html