Example usage for javax.jms Session AUTO_ACKNOWLEDGE

List of usage examples for javax.jms Session AUTO_ACKNOWLEDGE

Introduction

In this page you can find the example usage for javax.jms Session AUTO_ACKNOWLEDGE.

Prototype

int AUTO_ACKNOWLEDGE

To view the source code for javax.jms Session AUTO_ACKNOWLEDGE.

Click Source Link

Document

With this acknowledgment mode, the session automatically acknowledges a client's receipt of a message either when the session has successfully returned from a call to receive or when the message listener the session has called to process the message successfully returns.

Usage

From source file:com.fusesource.examples.activemq.SimpleConsumer.java

public static void main(String args[]) {
    Connection connection = null;

    try {/*from   w  ww  .  j  a v a 2s. c om*/
        // JNDI lookup of JMS Connection Factory and JMS Destination
        Context context = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME);
        Destination destination = (Destination) context.lookup(DESTINATION_NAME);

        connection = factory.createConnection();
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(destination);

        LOG.info("Start consuming messages from " + destination.toString() + " with "
                + MESSAGE_TIMEOUT_MILLISECONDS + "ms timeout");

        // Synchronous message consumer
        int i = 1;
        while (true) {
            Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS);
            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    LOG.info("Got " + (i++) + ". message: " + text);
                }
            } else {
                break;
            }
        }

        consumer.close();
        session.close();
    } catch (Throwable t) {
        LOG.error(t);
    } finally {
        // Cleanup code
        // In general, you should always close producers, consumers,
        // sessions, and connections in reverse order of creation.
        // For this simple example, a JMS connection.close will
        // clean up all other resources.
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOG.error(e);
            }
        }
    }
}

From source file:com.fusesource.examples.activemq.SimplePublisher.java

public static void main(String args[]) {
    Connection connection = null;

    try {/*w  w  w.j  a  v a2s. co  m*/
        // JNDI lookup of JMS Connection Factory and JMS Destination
        Context context = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME);

        connection = factory.createConnection();
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(DESTINATION_NAME);

        MessageProducer producer = session.createProducer(topic);

        producer.setTimeToLive(MESSAGE_TIME_TO_LIVE_MILLISECONDS);

        for (int i = 1; i <= NUM_MESSAGES_TO_BE_SENT; i++) {
            TextMessage message = session.createTextMessage(i + ". message sent");
            LOG.info("Sending to destination: " + DESTINATION_NAME + " this text: '" + message.getText());
            producer.send(message);
            Thread.sleep(MESSAGE_DELAY_MILLISECONDS);
        }

        // Cleanup
        producer.close();
        session.close();
    } catch (Throwable t) {
        LOG.error(t);
    } finally {
        // Cleanup code
        // In general, you should always close producers, consumers,
        // sessions, and connections in reverse order of creation.
        // For this simple example, a JMS connection.close will
        // clean up all other resources.
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOG.error(e);
            }
        }
    }
}

From source file:example.wildcard.Client.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();/* w w  w  .  j a  va  2s  . c  o m*/
    }
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;

    try {
        Topic senderTopic = new ActiveMQTopic(System.getProperty("topicName"));

        connection = connectionFactory.createConnection("admin", "password");

        Session senderSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = senderSession.createProducer(senderTopic);

        Session receiverSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);

        String policyType = System.getProperty("wildcard", ".*");
        String receiverTopicName = senderTopic.getTopicName() + policyType;
        Topic receiverTopic = receiverSession.createTopic(receiverTopicName);

        MessageConsumer receiver = receiverSession.createConsumer(receiverTopic);
        receiver.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage) {
                        String text = ((TextMessage) message).getText();
                        System.out.println("We received a new message: " + text);
                    }
                } catch (JMSException e) {
                    System.out.println("Could not read the receiver's topic because of a JMSException");
                }
            }
        });

        connection.start();
        System.out.println("Listening on '" + receiverTopicName + "'");
        System.out.println("Enter a message to send: ");

        Scanner inputReader = new Scanner(System.in);

        while (true) {
            String line = inputReader.nextLine();
            if (line == null) {
                System.out.println("Done!");
                break;
            } else if (line.length() > 0) {
                try {
                    TextMessage message = senderSession.createTextMessage();
                    message.setText(line);
                    System.out.println("Sending a message: " + message.getText());
                    sender.send(message);
                } catch (JMSException e) {
                    System.out.println("Exception during publishing a message: ");
                }
            }
        }

        receiver.close();
        receiverSession.close();
        sender.close();
        senderSession.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("When trying to close connection: ");
            }
        }
    }

}

From source file:example.composite.dest.Consumer.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();//from  w  ww.ja  v a 2  s . co m
    }
    System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 + "s");
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;

    try {

        connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("test-queue");
        Destination destinationFoo = session.createQueue("test-queue-foo");
        Destination destinationBar = session.createQueue("test-queue-bar");
        Destination destinationTopicFoo = session.createTopic("test-topic-foo");

        MessageConsumer consumer = session.createConsumer(destination);
        MessageConsumer consumerFoo = session.createConsumer(destinationFoo);
        MessageConsumer consumerBar = session.createConsumer(destinationBar);
        MessageConsumer consumerTopicFoo = session.createConsumer(destinationTopicFoo);

        int i = 0;
        while (true) {
            Message message = consumer.receive(TIMEOUT);

            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    System.out.println("Got " + i++ + ". message on test-queue: " + text);
                }
            } else {
                break;
            }

            message = consumerFoo.receive(TIMEOUT);

            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    System.out.println("Got " + i++ + ". message on test-queue-foo: " + text);
                }
            } else {
                break;
            }

            message = consumerBar.receive(TIMEOUT);

            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    System.out.println("Got " + i++ + ". message on test-queue-bar: " + text);
                }
            } else {
                break;
            }

            message = consumerTopicFoo.receive(TIMEOUT);

            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    System.out.println("Got " + i++ + ". message on test-topic-bar: " + text);
                }
            } else {
                break;
            }

        }

        consumer.close();
        session.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("Could not close an open connection...");
            }
        }
    }
}

From source file:example.transaction.Client.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();/*from   ww w  . ja va  2s .  c o m*/
    }
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;

    try {
        connection = connectionFactory.createConnection();
        connection.start();
        Topic destination = new ActiveMQTopic("transacted.client.example");

        Session senderSession = connection.createSession(TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Session receiverSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer receiver = receiverSession.createConsumer(destination);
        receiver.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if (message instanceof TextMessage) {
                    try {
                        String value = ((TextMessage) message).getText();
                        System.out.println("We received a new message: " + value);
                    } catch (JMSException e) {
                        System.out.println("Could not read the receiver's topic because of a JMSException");
                    }
                }
            }
        });

        MessageProducer sender = senderSession.createProducer(destination);

        connection.start();
        acceptInputFromUser(senderSession, sender);
        senderSession.close();
        receiverSession.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("Could not close an open connection...");
            }
        }
    }
}

From source file:com.fusesource.examples.activemq.SimpleProducer.java

public static void main(String args[]) {
    Connection connection = null;

    try {// w w  w. j  a  v a  2s.c  o m
        // JNDI lookup of JMS Connection Factory and JMS Destination
        Context context = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME);
        Destination destination = (Destination) context.lookup(DESTINATION_NAME);

        connection = factory.createConnection();
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(destination);

        producer.setTimeToLive(MESSAGE_TIME_TO_LIVE_MILLISECONDS);

        for (int i = 1; i <= NUM_MESSAGES_TO_BE_SENT; i++) {
            TextMessage message = session.createTextMessage(i + ". message sent");
            LOG.info("Sending to destination: " + destination.toString() + " this text: '" + message.getText());
            producer.send(message);
            Thread.sleep(MESSAGE_DELAY_MILLISECONDS);
        }

        // Cleanup
        producer.close();
        session.close();
    } catch (Throwable t) {
        LOG.error(t);
    } finally {
        // Cleanup code
        // In general, you should always close producers, consumers,
        // sessions, and connections in reverse order of creation.
        // For this simple example, a JMS connection.close will
        // clean up all other resources.
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOG.error(e);
            }
        }
    }
}

From source file:org.dawnsci.commandserver.mx.example.ActiveMQConsumer.java

public static void main(String[] args) throws Exception {

    ConnectionFactory connectionFactory = ConnectionFactoryFacade
            .createConnectionFactory("tcp://sci-serv5.diamond.ac.uk:61616");
    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue("testQ");

    final MessageConsumer consumer = session.createConsumer(queue);
    connection.start();//from w w w  .ja  v  a2 s . co  m

    while (true) { // You have to kill it to stop it!
        Message m = consumer.receive(1000);
        if (m != null) {
            if (m instanceof TextMessage) {
                TextMessage t = (TextMessage) m;
                System.out.println(t.getText());
                try {
                    ObjectMapper mapper = new ObjectMapper();
                    final SweepBean colBack = mapper.readValue(t.getText(), SweepBean.class);
                    System.out.println("Data collection found: " + colBack.getDataCollectionId());

                } catch (Exception ne) {
                    System.out.println(m + " is not a data collection.");
                }
            } else if (m instanceof ObjectMessage) {
                ObjectMessage o = (ObjectMessage) m;
                System.out.println(o.getObject());
            }
        }
    }
}

From source file:example.topic.Subscriber.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();//  ww  w .  jav  a 2s  .c  o m
    }
    System.out
            .println("\nWaiting to receive messages... Either waiting for END message or press Ctrl+C to exit");
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;
    final CountDownLatch latch = new CountDownLatch(1);

    try {

        connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createTopic("test-topic");

        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(new Subscriber(latch));

        latch.await();
        consumer.close();
        session.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("Could not close an open connection...");
            }
        }
    }
}

From source file:example.topic.durable.Subscriber.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();//from w  w w  . j  a v a 2s  .  c o  m
    }
    System.out
            .println("\nWaiting to receive messages... Either waiting for END message or press Ctrl+C to exit");
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;
    final CountDownLatch latch = new CountDownLatch(1);

    try {

        connection = connectionFactory.createConnection();
        String clientId = System.getProperty("clientId");
        connection.setClientID(clientId);

        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Topic destination = session.createTopic("test-topic");

        MessageConsumer consumer = session.createDurableSubscriber(destination, clientId);
        consumer.setMessageListener(new Subscriber(latch));

        latch.await();
        consumer.close();
        session.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("Could not close an open connection...");
            }
        }
    }
}

From source file:example.tempdest.ProducerRequestReply.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();//from w  ww  .ja v a 2s. c o m
    }
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;

    try {

        connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("test-queue");
        MessageProducer producer = session.createProducer(destination);
        Destination replyDest = session.createTemporaryQueue();

        // set up the consumer to handle the reply
        MessageConsumer replyConsumer = session.createConsumer(replyDest);
        replyConsumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                System.out.println("*** REPLY *** ");
                System.out.println(message.toString());
            }
        });

        TextMessage message = session.createTextMessage("I need a response for this, please");
        message.setJMSReplyTo(replyDest);

        producer.send(message);

        // wait for a response
        TimeUnit.SECONDS.sleep(2);
        producer.close();
        session.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("Could not close an open connection...");
            }
        }
    }
}