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:fr.xebia.springframework.jms.ManagedCachingConnectionFactoryTest.java

@Test
public void testMessageProducer() throws Exception {
    ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
            "vm://localhost?broker.persistent=false&broker.useJmx=true");
    ManagedConnectionFactory connectionFactory = new ManagedConnectionFactory(activeMQConnectionFactory);
    Connection connection = null;
    Session session = null;//from   www  .  j av a  2  s.c  o  m
    MessageProducer messageProducer = null;
    try {
        connection = connectionFactory.createConnection();
        assertEquals(1, connectionFactory.getActiveConnectionCount());
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        assertEquals(1, connectionFactory.getActiveSessionCount());
        Destination myQueue = session.createQueue("test-queue");
        messageProducer = session.createProducer(myQueue);

        assertEquals(1, connectionFactory.getActiveMessageProducerCount());

        messageProducer.send(myQueue, session.createTextMessage("test"));

        assertEquals(0, connectionFactory.getActiveMessageConsumerCount());
    } finally {
        JmsUtils.closeMessageProducer(messageProducer);
        assertEquals(0, connectionFactory.getActiveMessageProducerCount());
        JmsUtils.closeSession(session);
        assertEquals(0, connectionFactory.getActiveSessionCount());
        JmsUtils.closeConnection(connection);
        assertEquals(0, connectionFactory.getActiveConnectionCount());
    }
}

From source file:org.firstopen.custom.view.EventMonitorBean.java

public EventMonitorBean(String name) throws InfrastructureException {
    if (System.getSecurityManager() == null) {

        System.setSecurityManager(new SecurityManager());
    }//ww  w . j  ava  2 s  . c om

    JMSUtil.createQueue(MONITOR_NAME);
    connection = JMSUtil.getQueueConnection();
    try {
        queueSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageConsumer receiver = queueSession.createConsumer(JMSUtil.getQueue(MONITOR_NAME));

        receiver.setMessageListener(this);

        connection.start();
    } catch (JMSException e) {

        log.error("unable to start listener on Queue");
        throw new InfrastructureException(e);
    }
}

From source file:org.hawkular.apm.server.jms.AbstractPublisherJMS.java

@PostConstruct
public void init() {
    try {/*from   w  ww.j  a  v a 2  s.  c om*/
        InitialContext context = new InitialContext();
        ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("java:/APMJMSCF");
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // TODO: Transacted?
        Destination destination = (Destination) context.lookup(getDestinationURI());
        producer = session.createProducer(destination);
        connection.start();
    } catch (Exception e) {
        msgLog.errorFailedToInitPublisher(getDestinationURI(), e);
    }
}

From source file:org.apache.activemq.usecases.DurableSubscriptionHangTestCase.java

private void produceExpiredAndOneNonExpiredMessages() throws JMSException {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://" + brokerName);
    TopicConnection connection = connectionFactory.createTopicConnection();
    TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(topicName);
    MessageProducer producer = session.createProducer(topic);
    producer.setTimeToLive(TimeUnit.SECONDS.toMillis(1));
    for (int i = 0; i < 40000; i++) {
        sendRandomMessage(session, producer);
    }/*  w w  w .  jav  a  2s .co m*/
    producer.setTimeToLive(TimeUnit.DAYS.toMillis(1));
    sendRandomMessage(session, producer);
    connection.close();
    LOG.info("produceExpiredAndOneNonExpiredMessages done");
}

From source file:org.apache.activemq.bugs.AMQ7118Test.java

public void setupConsumerConnection() throws Exception {
    cConnection = ACTIVE_MQ_CONNECTION_FACTORY.createConnection();
    cConnection.setClientID("myClient1");
    cConnection.start();/* w  ww.  ja va  2  s. c  om*/
    cSession = cConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}

From source file:org.apache.flink.streaming.connectors.jms.JmsTopicSource.java

@Override
public void run(final SourceContext<T> context) throws Exception {
    TopicSession session = null;//from   w ww. jav a2 s.  c  om
    TopicSubscriber consumer = null;

    try {
        session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        consumer = session.createSubscriber(destination, messageSelector, false);

        connection.start();

        while (isRunning) {
            context.collect(convert(consumer.receive()));
        }
    } catch (JMSException e) {
        logger.error("Error receiving message from [{}]: {}", destination.getTopicName(),
                e.getLocalizedMessage());
        throw new UncategorizedJmsException(e);
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
        JmsUtils.closeSession(session);
    }
}

From source file:eu.learnpad.simulator.mon.manager.ResponseDispatcher.java

public static void sendResponse(ComplexEventException exception, String enablerName, String answerTopic) {
    try {//from  w  w w.  j  a va 2s .c o m
        publicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        connectionTopic = publishSession.createTopic(answerTopic);
        tPub = publishSession.createPublisher(connectionTopic);

        ObjectMessage sendMessage = publishSession.createObjectMessage();
        sendMessage.setObject((Serializable) exception);
        sendMessage.setStringProperty("DESTINATION", enablerName);
        tPub.publish(sendMessage);
    } catch (JMSException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.activemq.usecases.DurableSubscriberWithNetworkRestartTest.java

public void testSendOnAReceiveOnBWithTransportDisconnect() throws Exception {
    bridge(SPOKE, HUB);//ww  w.java2 s  .  c o  m
    startAllBrokers();

    verifyDuplexBridgeMbean();

    // Setup connection
    URI hubURI = brokers.get(HUB).broker.getTransportConnectors().get(0).getPublishableConnectURI();
    URI spokeURI = brokers.get(SPOKE).broker.getTransportConnectors().get(0).getPublishableConnectURI();
    ActiveMQConnectionFactory facHub = new ActiveMQConnectionFactory(hubURI);
    ActiveMQConnectionFactory facSpoke = new ActiveMQConnectionFactory(spokeURI);
    Connection conHub = facHub.createConnection();
    Connection conSpoke = facSpoke.createConnection();
    conHub.setClientID("clientHUB");
    conSpoke.setClientID("clientSPOKE");
    conHub.start();
    conSpoke.start();
    Session sesHub = conHub.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sesSpoke = conSpoke.createSession(false, Session.AUTO_ACKNOWLEDGE);

    ActiveMQTopic topic = new ActiveMQTopic("TEST.FOO");
    String consumerName = "consumerName";

    // Setup consumers
    MessageConsumer remoteConsumer = sesHub.createDurableSubscriber(topic, consumerName);
    sleep(1000);
    remoteConsumer.close();

    // Setup producer
    MessageProducer localProducer = sesSpoke.createProducer(topic);
    localProducer.setDeliveryMode(DeliveryMode.PERSISTENT);

    final String payloadString = new String(new byte[10 * 1024]);
    // Send messages
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        Message test = sesSpoke.createTextMessage("test-" + i);
        test.setStringProperty("payload", payloadString);
        localProducer.send(test);
    }
    localProducer.close();

    final String options = "?persistent=true&useJmx=true&deleteAllMessagesOnStartup=false";
    for (int i = 0; i < 2; i++) {
        brokers.get(SPOKE).broker.stop();
        sleep(1000);
        createBroker(new URI("broker:(tcp://localhost:61616)/" + SPOKE + options));
        bridge(SPOKE, HUB);
        brokers.get(SPOKE).broker.start();
        LOG.info("restarted spoke..:" + i);

        assertTrue("got mbeans on restart", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                return countMbeans(brokers.get(HUB).broker, "networkBridge", 20000) == (dynamicOnly ? 1 : 2);
            }
        }));
    }
}

From source file:eu.learnpad.simulator.mon.manager.ResponseDispatcher.java

public ResponseDispatcher(InitialContext initConn, TopicConnectionFactory connectionFact,
        HashMap<Object, ConsumerProfile> requestMap, LearnerAssessmentManager learnerAssessmentManager) {

    ResponseDispatcher.requestMap = requestMap;
    ResponseDispatcher.initConn = initConn;
    ResponseDispatcher.lam = learnerAssessmentManager;
    try {//from   ww w.  java2 s  .  co  m
        connection = connectionFact.createTopicConnection();
        publishSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    } catch (JMSException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.flink.streaming.connectors.jms.JmsQueueSource.java

@Override
public void run(final SourceContext<T> context) throws Exception {
    QueueSession session = null;/* w ww  .  j a v  a2  s . c  o  m*/
    QueueReceiver consumer = null;

    try {
        session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        consumer = session.createReceiver(destination, messageSelector);

        connection.start();

        while (isRunning) {
            context.collect(convert(consumer.receive()));
        }
    } catch (JMSException e) {
        logger.error("Error receiving message from [{}]: {}", destination.getQueueName(),
                e.getLocalizedMessage());
        throw new UncategorizedJmsException(e);
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
        JmsUtils.closeSession(session);
    }
}