Last active
August 29, 2015 14:04
-
-
Save hansjmelby/0f0287529406f802ddb8 to your computer and use it in GitHub Desktop.
Simple JMS client that uses delayed messaging in activeMQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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