Skip to content

Instantly share code, notes, and snippets.

@prassee
Created April 1, 2013 08:21
Show Gist options
  • Save prassee/5283799 to your computer and use it in GitHub Desktop.
Save prassee/5283799 to your computer and use it in GitHub Desktop.
a simple code to write a message to FFMQ queue and read it back
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import net.timewalker.ffmq3.FFMQConstants;
/**
*
* @author css99362
*/
public class Ffmq {
public static void queueMsgs() throws NamingException, JMSException {
// Create and initialize a JNDI context
Hashtable<String,String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, FFMQConstants.JNDI_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, "tcp://localhost:10002");
Context context = new InitialContext(env);
// Lookup a connection factory in the context
ConnectionFactory connFactory = (ConnectionFactory) context.lookup(FFMQConstants.JNDI_CONNECTION_FACTORY_NAME);
// Obtain a JMS connection from the factory
Connection conn = connFactory.createConnection();
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("TESTQUEUE");
// msg producer
MessageProducer messageProducer = session.createProducer(destination);
messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
messageProducer.send(session.createTextMessage("from one"));
// msg consumer
MessageConsumer consumer = session.createConsumer(destination);
Message message = consumer.receive(1000);
if(message instanceof TextMessage) {
TextMessage tm = (TextMessage) message;
System.out.println("obtained " + tm.getText());
}
session.close();
conn.close();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
queueMsgs();
} catch (NamingException | JMSException ex) {
Logger.getLogger(Ffmq.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment