Set Delivery Mode for MessageProducer - Java Message

Java examples for Message:JMS

Description

Set Delivery Mode for MessageProducer

Demo Code



import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Server implements MessageListener
{

  private Session session;
  private MessageProducer replyProducer;

  public Server()
  {//from   ww w .  j  a  v a 2s  .  c o  m
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://172.16.0.191:61616");
    Connection connection;
    try
    {
      connection = connectionFactory.createConnection();
      connection.start();
      session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Destination destination = session.createQueue("com.balitimes.QueueReply");
      MessageConsumer consumer = session.createConsumer(destination);
      replyProducer = session.createProducer(null);
      replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  
      consumer.setMessageListener(this);
    }
    catch (JMSException e)
    {
      // TODO: handle exception
    }
  }

  @Override
  public void onMessage(Message message)
  {

    try
    {
      System.out.println("server reply...." +  message.getJMSCorrelationID());
      TextMessage responMessage = this.session.createTextMessage();
      if (message instanceof TextMessage)
      {
        responMessage.setText("respone by server ## " + message.getJMSCorrelationID());
      } 
      responMessage.setJMSCorrelationID(message.getJMSCorrelationID());
      this.replyProducer.send(message.getJMSReplyTo(), responMessage);
    }
    catch (JMSException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

  public static void main(String[] args)
  {
    new Server();
  }
}

Related Tutorials