Use of activemq JMS in sending emails

Source: Internet
Author: User

 

Activemq is the most popular and powerful open-source message bus produced by Apache. Activemq is a JMS provider that fully supports the jms1.1 and J2EE 1.4 specifications. Although it has been a long time since the JMS specifications were introduced, however, JMS still plays a special role in today's J2EE applications.

Main features:

1. Write clients in multiple languages and protocols. Languages: Java, C, C ++, C #, Ruby, Perl, Python, and PHP. Application Protocol: openwire, stomp rest, WS notification, XMPP, amqp

2. Fully supports jms1.1 and J2EE 1.4 specifications (persistence, XA messages, transactions)

3. For spring support, activemq can be easily embedded into the system using spring, and also support spring features.

4. we have passed tests on common J2EE servers (such as Geronimo, JBoss 4, glassfish, and WebLogic), and configured JCA 1.5 Resource adaptors, activemq can be automatically deployed on any J2EE 1.4-compatible commercial server.

5. Support multiple transfer protocols: In-vm, TCP, SSL, NiO, UDP, jgroups, JXTA

6. support high-speed message persistence through JDBC and journal

7. The design ensures high-performance clusters, client-server, and point-to-point

8. Support for Ajax

9. Support integration with Axis

10. You can easily call the embedded JMS provider for testing.

 

1. Install and run activemq in Linux

 

(1) download activemq http://activemq.apache.org/download.html

Linux version of http://www.apache.org/dyn/closer.cgi? Bytes

 

Wget http://labs.renren.com/apache-mirror//activemq/apache-activemq/5.5.0/apache-activemq-5.5.0-bin.tar.gz

 

Extract tar zxvf apache-activemq-5.5.0-bin.tar.gz

 

(3) run the apache-activemq-5.5.0/bin/activemq start default port is 61616 can be viewed with netstat-an | grep 61616

 

(4) modify the port found in apache-activemq-5.5.0/CONF/activemq. xml

 

<Transportctor name = "openwire" uri = "TCP: // 0.0.0.0: 61616"/>

 

(5) set the user name and password to verify the Client Connection

 

In the apache-activemq-5.5.0/CONF/Credentials. properties file, change the user name and password

 

 

Activemq. Username = System

Activemq. Password = Manager

Guest. Password = Password

 

 

(6) Stop activemq and execute apache-activemq-5.5.0/bin/activemq stop

 

2. Use Java to call

 

(1) obtain the connectionfactory

 

Private Static connectionfactory; <br/>/** <br/> * Get the connection factory <br/> **/<br/> Public static connectionfactory getjmsconnectionfactory () {<br/> If (connectionfactory = NULL) {<br/> configmodel = configfactory. getconfigmodel (); <br/> // you can set the user name and password. <br/> connectionfactory = new activemqconnectionfactory (<br/> activemqconnection. default_user, <br/> activemqconnection. default_password, <br/> "TCP: // 192.168.0.3: 61616"); <br/>}< br/> return connectionfactory; <br/>} 

 

(2) send messages (an email model). This model is saved, so it is not time-consuming to send messages.

 

/** <Br/> * send an email object <br/> **/<br/> Public static void sendemailoperationmodel (emailmodel) {<br/> connectionfactory connfactory = getjmsconnectionfactory (); <br/> try {<br/> // connection between the JMS client and the JMS provider <br/> connection = connfactory. createconnection (); <br/> connection. start (); <br/> // session: a thread that sends or receives messages <br/> session = connection. createsession (Boolean. true, session. auto_acknowledge); <br/> // destination: the destination of the message to which the message is sent. <br/> // obtain the session. Note that the parameter value my-queue is the name of the query. <br/> destination Destination = session. createqueue (appconstants. jms_queue_name_email); <br/> // messageproducer: Message producer <br/> messageproducer producer = session. createproducer (destination); <br/> // set the expiration time <br/> // producer. settimetolive (3600*1000*24*2); <br/> // set persistence <br/> // producer. setdeliverymode (deliverymode. persistent); <br/> // send a message <br/> objectmessage = session. createobjectmessage (emailmodel); <br/> // send a message through the message producer <br/> producer. send (objectmessage); <br/> session. commit (); <br/> connection. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>} 

 

(3) trigger to send mail. This operation is asynchronous and does not affect the system performance. You need to start this thread to listen to messages at system startup, and then send emails.

 

Public class mailsendmanager extends thread {<br/> protected final log = logfactory. getlog (mailsendmanager. class); <br/> Public mailsendmanager () {<br/>}< br/>/** <br/> * Wait for receiving operation information and then send an email <br/> **/<br/> Public static void sendemail () {<br/> try {<br/> // connection between the JMS client and the JMS provider <br/> connection = jmsfactory. getjmsconnectionfactory (). createconnection (); <br/> connection. start (); <br/> // session: a thread that sends or receives messages <br/> session = connection. createsession (Boolean. false, session. auto_acknowledge); <br/> // destination: the destination of the message to which the message is sent. <br/> // get the session attention parameter value xingbo. xu-queue is a server's queue, which must be configured on the activemq console <br/> destination Destination = session. createqueue (appconstants. jms_queue_name_email); <br/> messageconsumer consumer = session. createconsumer (destination); <br/> // application blocking mode <br/>/* While (true) {<br/> message = consumer. receive (); <br/> If (Message instanceof objectmessage) {<br/> objectmessage omsg = (objectmessage) message; </P> <p> // emailmodel operationmodel = (emailmodel) omsg. getObject (); <br/> // send an email <br/> // mailsender. sendmail (operationmodel); <br/> system. out. println ("Send email OK --------------"); <br/>}< br/>}*/<br/> // apply the event model <br/> consumer. setmessagelistener (New messagelistener () {<br/> Public void onmessage (message) {<br/> try {<br/> If (Message instanceof objectmessage) {<br/> objectmessage omsg = (objectmessage) message; <br/> emailmodel operationmodel = (emailmodel) omsg. getObject (); <br/> // send an email <br/> mailsender. sendmail (operationmodel); <br/>}< br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>}); <br/> // session. close (); <br/> // connection. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> Public void run () {<br/> mailsendmanager. sendemail (); <br/>} 

 

 

 

 

 

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.