A sample (example) java class with source code to connect ActiveMQ queue and post /put meassages to the Queue. It connect to the queue and whenever any message comes to the queueue.
Suppose you need to develop a ActiveMQ client utility / program which
will post the messages to the Active MQ queue.
Then
following code will help you.
You can copy the following
code and use it in your project.
Using this code you will be
able to post the required all a queue of Active MQ
Library / Jar required to run this code is activemq-all.jar. You can
download from ActiveMQ web site,
package easycodeforall.zpagecode;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class WriteToActiveMQ implements ExceptionListener {
private String name = "";
private String iD = "";
private String QueueOrTopicNameForOutput = "";
private String URLOfOutputQueueOrTopicServer = "";
static int count = 1;
// private static Logger logger =
// Logger.getLogger(WriteToActiveMQ.class.getName());
public static void main(String args[]) throws InterruptedException {
}
ActiveMQConnectionFactory connectionFactory = null;
// Create a Connection
Connection connection;
// Create a Session
Session session = null;
Destination destination = null;
// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = null;
MessageProducer producer;
private void init() throws JMSException {
System.out.println("Sent message: 1");
}
public void postMessageToActiveMQ(String xmlMsgStrig) {
try {
System.out.println("Sent message: 14");
System.out.println("Sent message: 12");
System.out.println("Opening connection to write/post message=Q=" + QueueOrTopicNameForOutput);
// Create a ConnectionFactory
connectionFactory = new ActiveMQConnectionFactory(URLOfOutputQueueOrTopicServer);
System.out.println("Sent message: 10");
// Create a Connection
connection = connectionFactory.createConnection();
System.out.println("Sent message: 110");
connection.start();
// Create a Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
destination = session.createQueue(QueueOrTopicNameForOutput);
System.out.println("Sent message: 13");
// Create a MessageProducer from the Session to the Topic or Queue
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = session.createTextMessage(xmlMsgStrig);
count++;
// Tell the producer to send the message
System.out.println("Sent message: " + message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
System.out.println("able to sned message: 144");
System.out.println("Posted/Written message to Q=" + QueueOrTopicNameForOutput);
// Clean up
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
} finally {
System.out.println("Closing connfor Q=" + QueueOrTopicNameForOutput);
closeAll();
}
}
public synchronized void onException(JMSException ex) {
System.out.println("JMS Exception occured. Shutting down client.");
}
public void closeAll() {
try {
if (consumer != null)
consumer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}