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:org.easybatch.jms.JmsIntegrationTest.java

@Test
public void testJmsSupport() throws Exception {
    Context jndiContext = getJndiContext();
    QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext
            .lookup("QueueConnectionFactory");
    Queue queue = (Queue) jndiContext.lookup("q");

    QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
    QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueSender queueSender = queueSession.createSender(queue);
    queueConnection.start();//from  www.  jav  a2 s  .c o m

    //send a regular message to the queue
    TextMessage message = queueSession.createTextMessage();
    message.setText(MESSAGE_TEXT);
    queueSender.send(message);

    //send a poison record to the queue
    queueSender.send(new JmsPoisonMessage());

    Job job = aNewJob().reader(new JmsQueueRecordReader(queueConnectionFactory, queue))
            .filter(new JmsPoisonRecordFilter()).processor(new RecordCollector())
            .jobListener(new JmsQueueSessionListener(queueSession))
            .jobListener(new JmsQueueConnectionListener(queueConnection)).build();

    JobReport jobReport = JobExecutor.execute(job);

    assertThat(jobReport).isNotNull();
    assertThat(jobReport.getParameters().getDataSource()).isEqualTo(EXPECTED_DATA_SOURCE_NAME);
    assertThat(jobReport.getMetrics().getTotalCount()).isEqualTo(2);
    assertThat(jobReport.getMetrics().getFilteredCount()).isEqualTo(1);
    assertThat(jobReport.getMetrics().getSuccessCount()).isEqualTo(1);

    List<JmsRecord> records = (List<JmsRecord>) jobReport.getResult();

    assertThat(records).isNotNull().isNotEmpty().hasSize(1);

    JmsRecord jmsRecord = records.get(0);
    Header header = jmsRecord.getHeader();
    assertThat(header).isNotNull();
    assertThat(header.getNumber()).isEqualTo(1);
    assertThat(header.getSource()).isEqualTo(EXPECTED_DATA_SOURCE_NAME);

    Message payload = jmsRecord.getPayload();
    assertThat(payload).isNotNull().isInstanceOf(TextMessage.class);

    TextMessage textMessage = (TextMessage) payload;
    assertThat(textMessage.getText()).isNotNull().isEqualTo(MESSAGE_TEXT);

}

From source file:org.okj.commons.broker.SimpleMessageSubscriber.java

/** 
 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
 *//*ww  w .  j a v  a2s .  c o m*/
@Override
public void afterPropertiesSet() throws Exception {
    //bean??????
    try {
        if (connectionFactory != null) {
            //1. ?
            this.connection = ((TopicConnectionFactory) connectionFactory).createTopicConnection();

            //2. ??
            this.session = this.connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

            //3. 
            this.topic = session.createTopic(topicName);

            //4. ??
            TopicSubscriber subscriber = null;
            if (StringUtils.isNotBlank(selector)) {
                subscriber = session.createSubscriber(topic, selector, false); //?
            } else {
                subscriber = session.createSubscriber(topic);
            }
            subscriber.setMessageListener(this);

            //2. ??
            this.connection.start();

            LogUtils.info(LOGGER, "???topicName={0}", topicName);
        }
    } catch (JMSException ex) {
        LogUtils.error(LOGGER, "??", ex);
    }
}

From source file:fr.xebia.sample.springframework.jms.requestreply.RequestReplyClientInvoker.java

/**
 * Request/Reply SpringFramework sample.
 * /*from ww  w . j  a v a  2s .co m*/
 * @param request
 *            sent to the remote service
 * @return reply returned by the remote service
 * @throws JMSException
 */
public String requestReply(String request) throws JMSException {

    Connection connection = connectionFactory.createConnection();
    try {
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        try {
            MessageProducer messageProducer = session.createProducer(this.requestDestination);
            try {
                Message requestMessage = session.createTextMessage(request);
                requestMessage.setJMSReplyTo(this.replyToDestination);
                // requestMessage.setJMSCorrelationID(String.valueOf(random.nextLong()));

                messageProducer.send(requestMessage);
                String messageSelector = "JMSCorrelationID  LIKE '" + requestMessage.getJMSMessageID() + "'";

                MessageConsumer messageConsumer = session.createConsumer(this.replyToDestination,
                        messageSelector);
                TextMessage replyMessage = (TextMessage) messageConsumer.receive(timeoutInMillis);
                Assert.notNull(replyMessage, "Timeout waiting for jms response");
                logger.debug(
                        "requestReply " + "\r\nrequest : " + requestMessage + "\r\nreply : " + replyMessage);
                String result = replyMessage.getText();
                logger.debug("requestReply('" + request + "'): '" + result + "'");
                return result;
            } finally {
                JmsUtils.closeMessageProducer(messageProducer);
            }
        } finally {
            JmsUtils.closeSession(session);
        }
    } finally {
        JmsUtils.closeConnection(connection);
    }
}

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

public void setupProducerConnection() throws Exception {
    producerConnection = ACTIVE_MQ_CONNECTION_FACTORY.createConnection();

    //Small hack to be sure the message IDs are the same across platforms when testing
    ((ActiveMQConnection) producerConnection).getConnectionInfo()
            .setConnectionId(new ConnectionId("connection:" + (++idGenerator)));

    producerConnection.start();/*ww  w  .ja  v a 2 s  . c  o  m*/
    pSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}

From source file:org.chtijbug.drools.platform.runtime.servlet.historylistener.ServletJmsStorageHistoryListener.java

public void initJmsConnection() throws DroolsChtijbugException {

    int numberRetries = new Integer(this.numberRetriesString);
    int timeToWaitBetweenTwoRetries = new Integer(this.timeToWaitBetweenTwoRetriesString);
    String url = "tcp://" + this.platformServer + ":" + this.platformPort;
    ConnectionFactory factory = new ActiveMQConnectionFactory(url);
    Connection connection = null;
    JMSConnectionListener jmsConnectionListener = new JMSConnectionListener() {
        @Override//  w w  w . ja  va 2s .  c  o  m
        public void connected(Connection connection) throws JMSException, InterruptedException {
            try {
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                Queue queue = session.createQueue(platformQueueName);
                producer = session.createProducer(queue);
                jmsConnectedSemaphore.acquire();
                for (HistoryEvent cachedhistoryEvent : cachedHistoryEvents) {
                    ObjectMessage cachedmsg = session.createObjectMessage(cachedhistoryEvent);
                    producer.send(cachedmsg);
                }
                cachedHistoryEvents.clear();
                jmsConnected = true;
                jmsConnectedSemaphore.release();
            } catch (JMSException e) {
                LOG.error("ServletJmsStorageHistoryListener.initJmsConnection.JMSException", e);
                throw e;
            } catch (InterruptedException e) {
                LOG.error("ServletJmsStorageHistoryListener.initJmsConnection.InterruptedException", e);
                throw e;
            }

        }
    };
    CreateJMSConnectionThread createJMSConnectionThread = new CreateJMSConnectionThread(jmsConnectionListener,
            numberRetries, factory, timeToWaitBetweenTwoRetries);
    createJMSConnectionThread.start();
}

From source file:org.sakaiproject.kernel.messaging.JmsEmailMessageHandler.java

/**
 * {@inheritDoc}/*from  w  w  w  .  j av  a2 s  .c o  m*/
 *
 * @see org.sakaiproject.kernel.api.messaging.MessageHandler#handle(java.lang.String,
 *      java.lang.String, java.lang.String, javax.jcr.Node)
 */
public void handle(String userID, String filePath, String fileName, Node node) {
    try {
        InputStream inputStream = nodeFactory.getInputStream(filePath);
        String content = IOUtils.readFully(inputStream, "UTF-8");
        Connection conn = connFactory.createConnection();
        conn.setClientID("sakai.emailmessagehandler");
        Session clientSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination emailTopic = clientSession.createTopic(emailQueueName);
        MessageProducer client = clientSession.createProducer(emailTopic);
        ObjectMessage mesg = clientSession.createObjectMessage(content);
        mesg.setJMSType(emailJmsType);
        client.send(mesg);
    } catch (JMSException e) {
        log.error(e.getMessage(), e);
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
    } catch (JCRNodeFactoryServiceException e) {
        log.error(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:org.wso2.andes.systest.GlobalQueuesTest.java

/**
 * Test that setting messageCount takes affect on topics
 *
 * We send 10 messages and disconnect at 9
 *
 * @throws Exception/*ww  w . j av a  2  s.  co m*/
 */
public void testTopicConsumerMessageCount() throws Exception {
    MAX_QUEUE_MESSAGE_COUNT = 10;

    setConfig("messageCount", String.valueOf(MAX_QUEUE_MESSAGE_COUNT - 1), false);

    //Start the broker
    startBroker();

    topicConsumer(Session.AUTO_ACKNOWLEDGE, false);
}

From source file:com.oneops.controller.jms.InductorPublisher.java

/**
 * Inits the.//from w  ww  .  j a v a  2s. c  om
 *
 * @throws JMSException the jMS exception
 */
public void init() throws JMSException {
    connection = connFactory.createConnection();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    connection.start();
    logger.info(">>>>>>>>>>>>>>InductorPublisher initialized...");
}

From source file:com.jim.im.config.GenericMQConfig.java

@Bean
public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory, JmsProperties properties,
        MessageConverter messageConverter, DestinationResolver destinationResolver) {
    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
    jmsTemplate.setMessageConverter(messageConverter);
    jmsTemplate.setPubSubDomain(properties.isPubSubDomain());
    jmsTemplate.setDeliveryMode(Session.AUTO_ACKNOWLEDGE);
    if (destinationResolver != null) {
        jmsTemplate.setDestinationResolver(destinationResolver);
    }/*from w w w. j  a  v  a  2 s .  c om*/
    jmsTemplate.setDefaultDestinationName(IMConstant.MQ_FORWARD_TOPIC_NAME);
    return jmsTemplate;
}