JBoss AS and ActiveMQ

I’ve created 22+ minutes screencast about JBoss AS and ActiveMQ integration

In this screencast you’ll learn how to:

  1. Deploy ActiveMQ
  2. Run consumer/supplier samples
  3. Integrate JBoss AS 6 and ActiveMQ
  4. Enqueue TextMessage from EJB using ActiveMQ

Source code for session bean:

package src;

import java.util.Date;

import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Session Bean implementation class MySimplePeriodicalTask
 */
@Stateless
public class MySimplePeriodicalTask {

	/**
	 * Default constructor.
	 */
	public MySimplePeriodicalTask() {
		System.out.print("MySimplePeriodicalTask() ctor called");
	}

	@Schedule(hour = "*", minute = "*", second = "*/10")
	public void someServerTask() throws JMSException {

		Connection conn = null;
		Session session = null;
		MessageProducer producer = null;
		try {
			InitialContext context = new InitialContext();
			QueueConnectionFactory connFactory = (QueueConnectionFactory) (context
					.lookup("java:activemq/QueueConnectionFactory"));
			System.out.println(connFactory.getClass().toString());

			conn = connFactory.createConnection();
			conn.start();

			session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue queue = session.createQueue(this.getClass().toString()
					+ ".QUEUE");

			producer = session.createProducer(queue);
			producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

			String content = (new Date()).toString();
			TextMessage msg = session.createTextMessage(content);

			producer.send(msg);

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			producer.close();
			session.close();
			conn.close();
		}

		System.out.println("Task has been performed");
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: