tags:

views:

282

answers:

1

Hi guys,

I'm testing open MQ for send and receive messages in my project. I have no problem to configure it to send a synchronous message, but i can't find any way in the official documentation to configure the message to be consumed 15 minutes after the producer send a message, and continue call the consumer if an error appears.

offical documentation: http://dlc.sun.com/pdf/819-7757/819-7757.pdf

my method whom send a message

public void sendMessage(EntradaPrecomven entrada){

 try{

  Hashtable env = new Hashtable();

  env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
  env.put(Context.PROVIDER_URL, "file:///C:/mqteste");

  // Create the initial context.
  Context ctx = new InitialContext(env);

  // Look up the connection factory object in the JNDI object store.
  autenticisFactory = (ConnectionFactory) ctx.lookup(CF_LOOKUP_NAME);

  mdbConn = autenticisFactory.createConnection();
  mdbSession = mdbConn.createSession(false, Session.AUTO_ACKNOWLEDGE);

  Destination destination = (Destination) ctx.lookup(DEST_LOOKUP_NAME);

  MessageProducer myProducer = mdbSession.createProducer(destination);
  ObjectMessage outMsg = mdbSession.createObjectMessage(entrada);
  outMsg.setJMSRedelivered(Boolean.TRUE);
  myProducer.send(outMsg);

  consumidor = mdbSession.createConsumer(destination);
  MessageMDB myListener = new MessageMDB();
  consumidor.setMessageListener(myListener);
  mdbConn.start();
  mdbConn.close();

 }catch(Exception e){
  try {
   mdbSession.rollback();
  } catch (JMSException e1) {}
 }
}

My listener:

@Override
    public void onMessage(Message msg) {

     ObjectMessage objMessage = (ObjectMessage) msg;

     try {

      System.out.println("Received Phone Call:" + objMessage.getJMSRedelivered());
      throw new JMSException("TESTE");

     } catch (JMSException e) {
      e.printStackTrace();
     }
    }

So, when i call mdbConn.start() the sendMessage() is called, but i want to call 15 minutes after the call. And whatever it sendMessage() does, the message is always removed from the queue. How can i keep the messagen in queue to be called later ?

Thanks!

+1  A: 

The message is removed from the broker queue due to the fact that the session you are using is set to auto acknowledge.

mdbSession = mdbConn.createSession(false, Session.AUTO_ACKNOWLEDGE);

This will automatically send an acknowledgement to the broker that the listener has received a message for the consumer with which it is associated once the onMessage() method has executed to completion. This then results in the message being removed from the queue.

If you manually take over the acknowledgement process you can choose to only acknowledge the receipt of the message at a time of your choosing (be that 15 minutes later or whatever criteria you have for the consuming client).

Setting the Session Session.CLIENT_ACKNOWLEDGE will allow you to do this but then you will have to manually send an acknowledge in your consumer code. By calling acknowledge on the message msg.acknowledge() inside your onMessage() method within your listener.

This will then acknowledge the receipt of messages consumed within that session and remove them from the queue.

Pages 46 and 65 in the pdf you quoted are useful for more information on this as is the api

TomY