"JAVA.JMS" A simple JMS instance

Source: Internet
Author: User

Select Activemq.

To establish a simple Maven project, the Pom.xml is as follows:

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.gof</groupId> <artifactId>jms-test< /artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name> jms-test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourcee 
      ncoding>utf-8</project.build.sourceencoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1&lt ;/version> <scope>test</scope> </dependency> <!--Add by WXB--> &LT;DEP Endency> <groupId>org.apache.activemq</groupid> <artifactId>activemq-client</artifactId> <version>5.10.0</version> &L
 T;/dependency> </dependencies> </project>

To create a test class:

Package com.gof.jms.test;
Import javax.jms.Connection;
Import Javax.jms.ConnectionFactory;
Import Javax.jms.DeliveryMode;
Import javax.jms.Destination;
Import Javax.jms.Message;
Import Javax.jms.MessageConsumer;
Import Javax.jms.MessageProducer;
Import javax.jms.Session;

Import Javax.jms.TextMessage;

Import Org.apache.activemq.ActiveMQConnectionFactory;
	public class Simplemessagesendandreceiveapp {public static final String user = ' System ';
	public static final String password = "Manager";
	public static final String URL = "tcp://localhost:61616";
	public static final String queuename = "Test_queue";
	public static final String messagebody = "Hello jms!";
	public static Final Boolean transacted = false;
	
    public static Final Boolean persistent = false;
    	public static void Main (string[] args) {Connection Connection = null;
    	
    	Session session = NULL; try{//Create the connection connectionfactory connectionfactory = new Activemqconnectionfactory(user, password, URL);
            Connection = Connectionfactory.createconnection ();
            
            Connection.start ();
            Create the session session = Connection.createsession (transacted, Session.auto_acknowledge);
            
            Destination Destination = Session.createqueue (queuename);
            Create the producer MessageProducer producer = Session.createproducer (destination);
            if (persistent) {Producer.setdeliverymode (deliverymode.persistent);
            }else{Producer.setdeliverymode (deliverymode.non_persistent);
            
            }//Create text message message = Session.createtextmessage (MessageBody);
            Send the message producer.send (message);
            
            SYSTEM.OUT.PRINTLN ("Send message:" + (textmessage) message). GetText ()); Create the consumer Messageconsumer COnsumer = Session.createconsumer (destination);
            Blocking till receive the message Recvmessage = Consumer.receive ();
            
    	SYSTEM.OUT.PRINTLN ("Receive message:" + ((textmessage) recvmessage). GetText ());
    	}catch (Exception e) {e.printstacktrace ();
    		    }finally{try{//close sessions and connection if (session!= null) {session.close ();
    		    } if (connection!= null) {connection.close ();
    		}}catch (Exception e) {e.printstacktrace (); }}}<span style= "font-family:simsun;font-size:10px;" > </span>


To start the test:

Start Activemq and create a queue named Test_queue in the admin page (http://localhost:8161/admin):


Run the program step-by-step when calling:

Producer.send (message);

You can see ACTIVEMQ received this message:

You can also see the current active Producer for the queue:


When calling:

Consumer.receive ();

You can see that the message is consumed:


In the console, you can see the results of the output:

Send Message:hello jms!
Receive Message:hello jms!







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.