I’ve created 22+ minutes screencast about JBoss AS and ActiveMQ integration
In this screencast you’ll learn how to:
- Deploy ActiveMQ
- Run consumer/supplier samples
- Integrate JBoss AS 6 and ActiveMQ
- 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"); } }
Leave a Reply