Apache Message Queuing--ACTIVITEMQ

Source: Internet
Author: User
Tags gettext sendmsg

1 set whether the background process will have unintended effects, if you want to start the Activemq.bat OH

Package com.apache.activemq.test;   
Import javax.jms.Connection;   
Import Javax.jms.ConnectionFactory;   
Import Javax.jms.DeliveryMode;   
Import javax.jms.Destination;   
Import javax.jms.JMSException;   
Import Javax.jms.MessageConsumer;   
Import Javax.jms.MessageProducer;   
Import javax.jms.Session;   
  
Import Javax.jms.TextMessage;   
Import org.apache.activemq.ActiveMQConnection;   
  
Import Org.apache.activemq.ActiveMQConnectionFactory; public class HelloWorld {public static void main (String [] args) throws interruptedexception{thread (NE   
                W Producer (), true);   
        Thread.Sleep (2000);   
    Thread (new Consumer (), false); The public static void thread (Runnable Runnable, Boolean daemon) {thread brokerthread = new Thread (Runnab   
        Le);   
        Brokerthread.setdaemon (daemon);   
    Brokerthread.start ();   
    }} class Producer implements runnable{private static final int send_number = 5;@Override public void Run () {connectionfactory connfactory;   
        Connection conn = null;   
        Session session;   
        Destination Destination;   
           
        MessageProducer Msgproducer; Constructs a ConnectionFactory instance object connfactory = new Activemqconnectionfactory (Activemqconne Ction. Default_user, Activemqconnection.default_password, "tcp://localhost:   
        61616 ");   
            try{//Get Connection object from construction Factory conn = Connfactory.createconnection ();   
            Start Connection Conn.start ();   
            Get Connected Session session = Conn.createsession (Boolean.true,session.auto_acknowledge);   
            Get connection Destination Destination = Session.createqueue ("Villasy-queue");   
            Message producer Msgproducer = Session.createproducer (destination);   
   Set not persisted         Msgproducer.setdeliverymode (deliverymode.non_persistent);   
            Constructs and sends a message sendmsg (Session,msgproducer);   
        Session.commit ();   
        }catch (Exception ex) {ex.printstacktrace ();   
                }finally{if (null! = conn) try {conn.close ();   
                } catch (JMSException e) {e.printstacktrace (); }}} public static void Sendmsg (Session session,messageproducer msgproducer) throws Jmse xception{for (int i=1;i<send_number;i++) {TextMessage txtmsg = session.createtextmessage ("Send   
            Interest: "+i);   
            System.out.println (Txtmsg.gettext ());   
        Msgproducer.send (TXTMSG); }}} class Consumer implements runnable,javax.jms.exceptionlistener{@Override public void R  Un () {///ConnectionFactory: Connection Factory, JMS uses it to create a connection 
        ConnectionFactory ConnectionFactory;   
        CONNECTION:JMS client-to-JMS Provider connection Connection Connection = null;   
        Session: A thread that sends or receives a message session session;   
        Destination: The destination of the message, to whom the message is sent.   
        Destination Destination;   
  
        Consumer, message recipient Messageconsumer consumer;   
                ConnectionFactory = new Activemqconnectionfactory (Activemqconnection.default_user,   
        Activemqconnection.default_password, "tcp://localhost:61616");   
            try {//Construct get Connection object from factory connection = Connectionfactory.createconnection ();   
            Start Connection.start (); Get operation Connection session = Connection.createsession (Boolean.false, Session.auto   
            _acknowledge);   
            Gets the Session0 queue destination = Session.createqueue ("Villasy-queue"); CoNsumer = Session.createconsumer (destination);   
                while (true) {TextMessage message = (textmessage) consumer.receive (1000);   
                if (null! = message) {SYSTEM.OUT.PRINTLN ("Receive Message" + Message.gettext ());   
                } else {break;   
        }}} catch (Exception e) {e.printstacktrace ();   
            } finally {try {if (null! = connection) connection.close ();  } catch (Throwable ignore) {}}} @Override public synchronized void  Onexception (jmsexception arg0) {System.out.println ("JMS Exception occured.
 Shutting down client. ");}}


Send message: 1 Send message: 2 Send message: 3 Send message:
4
Receive message Send message: 1
Receive message Send message: 2 Receive message Send message: 3 Receive message Send message: 4



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.