Archive

Archive for April, 2012

Send/Receive JMS Message in java

April 9, 2012 Comments off

Basic code JMS Code for sending and receiving theĀ  JMS Messages

</pre>
import java.io.Serializable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import com.fast.mailer.pojo.MailObj;

public class JMSAdapter
{
private static String url = "tcp://localhost:61616";
private static String subject = "mailerQueue";

public void send(Serializable message) throws Exception
{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);

MessageProducer producer = session.createProducer(destination);
ObjectMessage objMessage = session.createObjectMessage(message);

producer.send(destination,objMessage);
System.out.println("Sent message ObjectMessage");

connection.close();
}

public void receive() throws Exception
{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
MessageConsumer consumer = session.createConsumer(destination);

Message message = consumer.receive();
if (message instanceof TextMessage)
{
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message '" + textMessage.getText() + "'");
}else if(message instanceof ObjectMessage)
{
ObjectMessage objMessage = (ObjectMessage) message;

MailObj mailObj = (MailObj)objMessage.getObject();

System.out.println("Received ObjectMessage '" + mailObj.toString() + "'");
}
connection.close();
}
}
<pre>

Code to test the above code

</pre>
import java.util.ArrayList;
import java.util.List;

import com.fast.mailer.pojo.MailObj;

public class TestMailer
{
public TestMailer()
{
try
{
MailObj mailObj = new MailObj();
mailObj.setFrom("alerts@mymonitor.in");

List&lt;String&gt; to = new ArrayList&lt;String&gt;();
to.add("talluri.amara@gmail.com");
mailObj.setTo(to);
mailObj.setSubject("Test mail");
mailObj.setContent("Test Mail");
mailObj.setContentType(MailObj.CONTENT_TYPE_TEXT_HTML);

JMSAdapter adapter = new JMSAdapter();
adapter.send(mailObj);

} catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
new TestMailer();
}
}
<pre>

For this code add the below dependency to your pom file


		&lt;dependency&gt;
			&lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
			&lt;artifactId&gt;activemq-all&lt;/artifactId&gt;
			&lt;version&gt;${activemq.version}&lt;/version&gt;
		&lt;/dependency&gt;

or add activemq-all-5.5.1.jar file to your java build path.

Advertisements
Categories: Java
%d bloggers like this: