Java Code Examples for javax.jms.Message.acknowledge()

The following are Jave code examples for showing how to use acknowledge() of the javax.jms.Message class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: aem-orchestrator   File: OrchestratorMessageListener.java   Source Code and License Vote up 6 votes
@Override
public void onMessage(Message message) {
	try {
		if (message != null) {
			logger.info("Message received " + message.getJMSMessageID());

			boolean removeMessageFromQueue = messageHandler.handleMessage(message);
			
			//Acknowledging the message with remove it from the queue
			if(removeMessageFromQueue) {
			    logger.info("Acknowledged message (removing from queue): " + message.getJMSMessageID());
			    message.acknowledge();
			} else {
			    logger.info("Leaving message " + message.getJMSMessageID() + " on the queue");
			}
		} else {
			logger.info("Null message received");
		}

	} catch (Exception e) {
		logger.error("Error while recieving message", e);
	}
}
 
Example 2
Project: oneops   File: SensorListener.java   Source Code and License Vote up 6 votes
/**
 * takes the jms message
 */
public void onMessage(Message msg) {
	try {
		
		if (msg instanceof ObjectMessage) {
			BasicEvent event = (BasicEvent)((ObjectMessage)msg).getObject();
			if (logger.isDebugEnabled() || cisToLog.contains(event.getCiId())) {
				logger.info(gson.toJson(event));
			}
			sensorHeartBeat.timeStampIt(event.getChannel());
			sensorHeartBeat.timeStampIt(BasicEvent.DEFAULT_CHANNEL);
			sensor.sendCEPEvent(event);
		}
		msg.acknowledge();
	} catch (JMSException e) {
		logger.info("caught Exception in onMessage",e);

	}
}
 
Example 3
Project: ats-framework   File: JmsClient.java   Source Code and License Vote up 5 votes
private void doCleanupQueue( final Session session, final Destination destination ) throws JMSException {

        try {
            MessageConsumer consumer = session.createConsumer(destination);
            Message message = null;
            do {
                message = consumer.receiveNoWait();
                if (message != null) {
                    message.acknowledge();
                }
            } while (message != null);
        } finally {
            releaseSession(false);
        }
    }
 
Example 4
Project: oneops   File: AntennaListener.java   Source Code and License Vote up 5 votes
/**
 * (non-Javadoc)
 *
 * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
 */
public void onMessage(Message msg) {
    msgs.mark();
    Context tc = msgTime.time();
    try {
        NotificationMessage notify = null;
        if (msg instanceof TextMessage) {
            try {
                notify = parseMsg((TextMessage) msg);
            } catch (JsonParseException e) {
                logger.error("Got the bad message, not a valid json format - \n"
                        + ((TextMessage) msg).getText() + "\n" + e.getMessage());
                msg.acknowledge();
            }
        } else if (msg instanceof ObjectMessage) {
            notify = (NotificationMessage) ((ObjectMessage) msg).getObject();
        }
        if (notify != null) {
            logger.debug("Notification message received: " + notify.getText());
            if (notify.getTimestamp() == 0) {
                notify.setTimestamp(System.currentTimeMillis());
            }
            dispatcher.dispatch(notify);
        }
        msg.acknowledge();

    } catch (Exception ex) {
        logger.error("Can't process the notification message.", ex);
    } finally {
        tc.stop();
    }
}
 
Example 5
Project: tangyuan2   File: ActiveMqReceiver.java   Source Code and License Vote up 5 votes
private void processMessage(Message message) throws Throwable {
	if (null == message) {
		// log.error("message is null...");
		return;
	}
	try {
		XCO xcoMessage = null;
		if (message instanceof TextMessage) {
			TextMessage textMessage = (TextMessage) message;
			String text = textMessage.getText();
			xcoMessage = XCO.fromXML(text);
		} else {
			// TODO
		}

		log.info("received a message from " + typeStr + "[" + queue.getName() + "]: " + xcoMessage);

		boolean execResult = true;
		if (null != xcoMessage) {
			execResult = exec(service, xcoMessage, binding);
		}

		if (execResult && Session.SESSION_TRANSACTED == session.getAcknowledgeMode()) {
			session.commit();
		} else if (execResult && Session.CLIENT_ACKNOWLEDGE == session.getAcknowledgeMode()) {
			message.acknowledge();
		}

		if (!execResult && Session.SESSION_TRANSACTED == session.getAcknowledgeMode()) {
			session.rollback();
		}
	} catch (Throwable e) {
		if (Session.SESSION_TRANSACTED == session.getAcknowledgeMode()) {
			session.rollback();
		}
		throw e;
	}
}
 
Example 6
Project: message-broker   File: QueueConsumerTest.java   Source Code and License Vote up 4 votes
@Parameters({ "broker-port", "admin-username", "admin-password", "broker-hostname" })
@Test
public void testConsumerWithBasicReject(String port,
                                        String adminUsername,
                                        String adminPassword,
                                        String brokerHostname) throws Exception {
    System.setProperty("AndesAckWaitTimeOut", "5000");
    String queueName = "testConsumerWithBasicReject";
    InitialContext initialContextForQueue = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withQueue(queueName)
            .build();

    ConnectionFactory connectionFactory
            = (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    // publish message
    Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = producerSession.createQueue(queueName);
    MessageProducer producer = producerSession.createProducer(queue);

    producer.send(producerSession.createTextMessage("Test message for reject test"));
    producerSession.close();

    // Consume published messages
    Session subscriberSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
    MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);

    Message message = consumer.receive(5000);
    Assert.assertNotNull(message, "Message was not received");

    message = consumer.receive(10000);
    Assert.assertNotNull(message, "Requeued Message was not received");
    Assert.assertTrue(message.getJMSRedelivered(), "Redelivered flag was not set");
    message.acknowledge();

    connection.close();
}
 
Example 7
Project: message-broker   File: QueueConsumerTest.java   Source Code and License Vote up 4 votes
@Parameters({ "broker-port", "admin-username", "admin-password", "broker-hostname" })
@Test
public void testConsumerWithBasicRecover(String port,
                                        String adminUsername,
                                        String adminPassword,
                                        String brokerHostname) throws Exception {
    String queueName = "testConsumerWithBasicRecover";
    InitialContext initialContextForQueue = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withQueue(queueName)
            .build();

    ConnectionFactory connectionFactory
            = (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    // publish message
    Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = producerSession.createQueue(queueName);
    MessageProducer producer = producerSession.createProducer(queue);

    TextMessage firstMessage = producerSession.createTextMessage("First message for reject test");
    TextMessage secondMessage = producerSession.createTextMessage("Second message for reject test");
    String correlationIdOne = "1";
    String correlationIdTwo = "2";
    firstMessage.setJMSCorrelationID(correlationIdOne);
    secondMessage.setJMSCorrelationID(correlationIdTwo);
    producer.send(firstMessage);
    producer.send(secondMessage);
    producerSession.close();

    // Consume published messages
    Session subscriberSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
    MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);

    Message message = consumer.receive(5000);
    Assert.assertNotNull(message, "Message was not received");

    subscriberSession.recover();

    // Message order can change after recovering
    for (int i = 0; i < 2; i++) {
        message = consumer.receive(5000);
        Assert.assertNotNull(message, "Requeued message was not received");

        if (correlationIdOne.equals(message.getJMSCorrelationID())) {
            Assert.assertTrue(message.getJMSRedelivered(), "Redelivered flag was set in second message" + message);
        } else {
            Assert.assertFalse(message.getJMSRedelivered(), "Redelivered flag was set in first message");
        }

        message.acknowledge();
    }

    connection.close();
}