Skip to content

Instantly share code, notes, and snippets.

@hansjmelby
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hansjmelby/0f0287529406f802ddb8 to your computer and use it in GitHub Desktop.
Save hansjmelby/0f0287529406f802ddb8 to your computer and use it in GitHub Desktop.
Simple JMS client that uses delayed messaging in activeMQ
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ScheduledMessage;
public class SimpleDelayExample {
public static void main(String[] args) throws JMSException {
// Create a ConnectionFactory
String url = ActiveMQConnection.DEFAULT_BROKER_URL;
//ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("test msg");
long time = 5 * 1000;
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
// Tell the producer to send the message
// Clean up
session.close();
connection.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment