Activemq Using Tutorials

Source: Internet
Author: User

ActiveMQ is the most popular, powerful, open source messaging bus that Apache has produced. ActiveMQ is a JMS provider implementation that fully supports the JMS1.1 and the Java EE 1.4 specification, although the JMS specification has been around for a long time, but JMS still plays a special role in the middle of today's Java EE applications.

ACTIVEMQ Feature List

1. Write clients in multiple languages and protocols. Languages: Java, C, C + +, C #, Ruby, Perl, Python, PHP. Application protocol: Openwire,stomp REST,WS NOTIFICATION,XMPP,AMQP
2. Full support for JMS1.1 and the Java EE 1.4 specification (persistence, XA messages, transactions)
3. Support for spring, ACTIVEMQ can easily be embedded into the system using spring, but also support the features of Spring2.0
4. Tested by common Java EE servers (such as Geronimo,jboss 4, glassfish,weblogic), with the configuration of the JCA 1.5 resource adaptors, Allows ACTIVEMQ to automatically deploy to any compatible Java 1.4 Business Server
5. Support multiple transfer protocols: In-vm,tcp,ssl,nio,udp,jgroups,jxta
6. Support for high-speed message persistence through JDBC and journal
7. Designed to ensure a high-performance cluster, client-server, peer-to-peer
8. Support Ajax
9. Support for integration with axis
10. It is easy to call the embedded JMS provider for testing

1: Download ActiveMQ 5.6.0 Release

Http://activemq.apache.org/download.html

Put it on the D drive.



2: Run APACHE-ACTIVEMQ Service: double-click Activemq.bat



3: Effect



4: Required Jar Package

5:spring configuration file Applicationcontext.xml

<?xml version= "1.0"  encoding= "UTF-8"? ><! doctype beans public  "-//spring//dtd bean//en"   "http://www.springframework.org/dtd/ Spring-beans.dtd "><beans>    <bean id=" ConnectionFactory " class=" Org.apache.activemq.ActiveMQConnectionFactory ">        <property  name= "Brokerurl" >            <value >tcp://localhost:61616?wireFormat.maxInactivityDuration=0</value>         </property>    </bean>    <bean id= " Jmstemplate " class=" Org.springframework.jms.core.JmsTemplate ">         <property name= "ConnectionFactory" >             <ref bean= "ConnectionFactory"/>        </property>    </bean>     <bean id= "Destination"  class= "Org.apache.activemq.command.ActiveMQQueue" >         <constructor-arg index= "0" >             <value>MessageQueue</value>         </constructor-arg>    </bean></beans>

At this point is the main configuration of ACTIVEMQ service information and implementation of SPRINGJMS corresponding interface

6: Message Creator

package test;import javax.jms.jmsexception;import javax.jms.message;import  javax.jms.session;import org.springframework.jms.core.messagecreator;/** *  Message Creator  *  user: liuwentao * time: 12-6-14  Morning 11:31 */public class  Mymessagecreator implements messagecreator {    public int n  = 0;    private static String str1 =  "This is section  ";     private static String str2 =  "  Test messages! ";    private string str = " ";     @Override      public message createmessage (session paramsession)  throws  Jmsexception {        system.out.println ("MyMessageCreator   n= " + n);        if (n == 9)  {            // In this example, the 9th call is sent with the end message             return  Paramsession.createtextmessage ("End");        }         str = str1 + n + str2;         return paramsession.createtextmessage (str);     }}

7: Send Message Party

Package test;import javax.jms.destination;import org.springframework.context.applicationcontext ;import org.springframework.context.support.classpathxmlapplicationcontext;import  org.springframework.jms.core.jmstemplate;/** *  Send Message Party  * User: liuwentao *  time: 12-6-14  Morning 11:29 */public class messagesender extends thread {     public static void main (string args[])  throws Exception  {        String[] configLocations = new  string[] {"Test/applicationcontext.xml"};         Applicationcontext context = new classpathxmlapplicationcontext (configLocations);         JmsTemplate jmsTemplate =  (jmstemplate)   Context.getbean ("Jmstemplate");         destination destination =  (Destination)  context.getbean ("Destination");         for  (int i = 1; i < 100;  i++)  {            system.out.println (" Send  i= " + i);             //Message Creator             mymessagecreator mymessagecreator  = new mymessagecreator ();             myMessageCreator.n = i;             jmstemplate.send (Destination, mymessagecreator);             sleep (10000);//10 seconds to send next message         }     }}

8: Message Receiver

package test;import javax.jms.destination;import javax.jms.textmessage;import  org.springframework.context.applicationcontext;import  org.springframework.context.support.classpathxmlapplicationcontext;import  org.springframework.jms.core.jmstemplate;/** *  Message Receivers  * User: liuwentao *  time: 12-6-14  Morning 11:32 */public class messagereciver{    public  static void main (string args[])  throws Exception {         string[] configlocations = new string[] {"test/ Applicationcontext.xml "};        applicationcontext context  = new classpathxmlapplicationcontext (configlocations);         JmsTemplate jmsTemplate =  (jmstemplate)  context.getbean ("Jmstemplate");         Destination destination =  (Destination)  context.getbean ("Destination");         TextMessage msg = null;         //whether to continue receiving messages         boolean iscontinue  = true;        while  (iscontinue)  {             msg =  (TextMessage)   Jmstemplate.receive (destination);             System.out.println ("received message  :"  + msg.gettext ());             if  (Msg.gettext (). Equals ("End"))  {                 isContinue = false;            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Receive exit message, program to exit!") ");            }      &NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("The program exited!") ");     }}

9: Test

Run the sender and receiver (in order) the main file, the effect is as follows:





Note: If the receiver starts late or the sender shuts down, the receiver will receive all the data normally

Activemq Using Tutorials

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.