List of usage examples for javax.jms MessageListener MessageListener
MessageListener
From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java
private void addBlockrequestListener() throws JMSException { addMessageListener("blockRequest", new MessageListener() { @Override/*w w w.j av a 2s . c o m*/ public void onMessage(Message arg0) { BytesMessage o = (BytesMessage) arg0; try { byte[] body = new byte[(int) o.getBodyLength()]; o.readBytes(body); String hash = new Hash(BCSAPIMessage.Hash.parseFrom(body).getHash(0).toByteArray()).toString(); Block b = getBlock(hash); if (b != null) { reply(o.getJMSReplyTo(), b.toProtobuf().toByteArray()); } else { reply(o.getJMSReplyTo(), null); } } catch (Exception e) { log.trace("Rejected invalid block request ", e); } } }); }
From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java
private void addNewBlockListener() throws JMSException { addMessageListener("newBlock", new MessageListener() { @Override//from w w w . jav a 2 s. c o m public void onMessage(Message arg0) { BytesMessage o = (BytesMessage) arg0; try { byte[] body = new byte[(int) o.getBodyLength()]; o.readBytes(body); Block block = Block.fromProtobuf(BCSAPIMessage.Block.parseFrom(body)); block.computeHash(); sendBlock(block); reply(o.getJMSReplyTo(), null); } catch (Exception e) { BCSAPIMessage.ExceptionMessage.Builder builder = BCSAPIMessage.ExceptionMessage.newBuilder(); builder.setBcsapiversion(1); builder.addMessage(e.getMessage()); try { reply(o.getJMSReplyTo(), builder.build().toByteArray()); } catch (JMSException e1) { log.error("Can not send reply ", e1); } } } }); }
From source file:org.wso2.mb.integration.tests.amqp.functional.PerMessageAcknowledgementsTestCase.java
/** * This test publishes 10 messages and the subscriber rejects first 4 messages and then wait for the redelivered * message./*from www .ja v a 2 s.com*/ * * @throws AndesClientConfigurationException * @throws XPathExpressionException * @throws IOException * @throws JMSException * @throws AndesClientException * @throws NamingException */ @Test(groups = { "wso2.mb", "queue" }) public void firstFewUnacknowledgeMessageListenerPerAckTestCase() throws AndesClientConfigurationException, XPathExpressionException, IOException, JMSException, AndesClientException, NamingException { long sendCount = 10; final List<String> receivedMessages = new ArrayList<>(); // Creating a consumer client configuration AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(), ExchangeType.QUEUE, "firstFewUnacknowledgePerAckQueue"); consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE); consumerConfig.setAsync(false); // Creating a publisher client configuration AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration( getAMQPPort(), ExchangeType.QUEUE, "firstFewUnacknowledgePerAckQueue"); publisherConfig.setNumberOfMessagesToSend(sendCount); // Creating clients AndesClient consumerClient = new AndesClient(consumerConfig, true); final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0); MessageConsumer receiver = andesJMSConsumer.getReceiver(); receiver.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; if (Integer.parseInt(textMessage.getText().split("#")[1]) >= 4 || receivedMessages.contains(textMessage.getText())) { message.acknowledge(); } receivedMessages.add(textMessage.getText()); andesJMSConsumer.getReceivedMessageCount().incrementAndGet(); } catch (JMSException e) { throw new RuntimeException("Exception occurred when receiving messages.", e); } } }); AndesClient publisherClient = new AndesClient(publisherConfig, true); MessageProducer sender = publisherClient.getPublishers().get(0).getSender(); for (int i = 0; i < sendCount; i++) { TextMessage textMessage = publisherClient.getPublishers().get(0).getSession() .createTextMessage("#" + Integer.toString(i)); sender.send(textMessage); } AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME); log.info("Received Messages : " + receivedMessages); for (int i = 0; i < sendCount; i++) { Assert.assertEquals(receivedMessages.get(i), "#" + Integer.toString(i), "Invalid messages received. #" + Integer.toString(i) + " expected."); } Assert.assertEquals(receivedMessages.get(10), "#0", "Invalid messages received. #0 expected."); Assert.assertEquals(receivedMessages.get(11), "#1", "Invalid messages received. #1 expected."); Assert.assertEquals(receivedMessages.get(12), "#2", "Invalid messages received. #2 expected."); Assert.assertEquals(receivedMessages.get(13), "#3", "Invalid messages received. #3 expected."); Assert.assertEquals(receivedMessages.size(), sendCount + 4, "Message receiving failed."); }
From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java
private void addNewTransactionListener() throws JMSException { addMessageListener("newTransaction", new MessageListener() { @Override/*from w w w. j a va 2s. c o m*/ public void onMessage(Message arg0) { BytesMessage o = (BytesMessage) arg0; try { byte[] body = new byte[(int) o.getBodyLength()]; o.readBytes(body); Transaction transaction = Transaction.fromProtobuf(BCSAPIMessage.Transaction.parseFrom(body)); transaction.computeHash(); sendTransaction(transaction); reply(o.getJMSReplyTo(), null); } catch (Exception e) { BCSAPIMessage.ExceptionMessage.Builder builder = BCSAPIMessage.ExceptionMessage.newBuilder(); builder.setBcsapiversion(1); builder.addMessage(e.getMessage()); try { reply(o.getJMSReplyTo(), builder.build().toByteArray()); } catch (JMSException e1) { log.error("Can not send reply ", e1); } } } }); }
From source file:org.wso2.mb.integration.tests.amqp.functional.PerMessageAcknowledgementsTestCase.java
/** * This test publishes 10 messages and the subscriber rejects the 8th message and then wait for the redelivered * message./*from ww w . ja v a 2 s.c om*/ * * @throws AndesClientConfigurationException * @throws XPathExpressionException * @throws IOException * @throws JMSException * @throws AndesClientException * @throws NamingException */ @Test(groups = { "wso2.mb", "queue" }) public void unacknowledgeMiddleMessageMessageListenerPerAckTestCase() throws AndesClientConfigurationException, XPathExpressionException, IOException, JMSException, AndesClientException, NamingException { long sendCount = 10; final List<String> receivedMessages = new ArrayList<>(); // Creating a consumer client configuration AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(), ExchangeType.QUEUE, "unacknowledgeMiddleMessagePerAckQueue"); consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE); consumerConfig.setAsync(false); // Creating a publisher client configuration AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration( getAMQPPort(), ExchangeType.QUEUE, "unacknowledgeMiddleMessagePerAckQueue"); publisherConfig.setNumberOfMessagesToSend(sendCount); // Creating clients AndesClient consumerClient = new AndesClient(consumerConfig, true); final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0); MessageConsumer receiver = andesJMSConsumer.getReceiver(); receiver.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; if (!textMessage.getText().equals("#7") || receivedMessages.contains(textMessage.getText())) { message.acknowledge(); } receivedMessages.add(textMessage.getText()); andesJMSConsumer.getReceivedMessageCount().incrementAndGet(); } catch (JMSException e) { throw new RuntimeException("Exception occurred when receiving messages.", e); } } }); AndesClient publisherClient = new AndesClient(publisherConfig, true); MessageProducer sender = publisherClient.getPublishers().get(0).getSender(); for (int i = 0; i < sendCount; i++) { TextMessage textMessage = publisherClient.getPublishers().get(0).getSession() .createTextMessage("#" + Integer.toString(i)); sender.send(textMessage); } AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME); log.info("Received Messages : " + receivedMessages); for (int i = 0; i < sendCount; i++) { Assert.assertEquals(receivedMessages.get(i), "#" + Integer.toString(i), "Invalid messages received. #" + Integer.toString(i) + " expected."); } Assert.assertEquals(receivedMessages.get(10), "#7", "Invalid messages received. #7 expected."); Assert.assertEquals(receivedMessages.size(), sendCount + 1, "Message receiving failed."); }
From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java
/** * This test publishes 10 messages and the subscriber rejects a message after each 3 received messages and then wait * for the redelivered message.//from w w w . j a v a 2 s.co m * <p/> * The redelivered message is tested against the same message content with the original message and the timestamps * are also checked against the original message timestamp to make sure that the message was delayed. * * @throws AndesClientConfigurationException * @throws XPathExpressionException * @throws IOException * @throws JMSException * @throws AndesClientException * @throws NamingException */ @Test(groups = { "wso2.mb", "queue" }) public void oneByOneUnacknowledgeMessageListenerTestCase() throws AndesClientConfigurationException, XPathExpressionException, IOException, JMSException, AndesClientException, NamingException { long sendCount = 10; final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>(); // Creating a consumer client configuration AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(), ExchangeType.QUEUE, "oneByOneUnacknowledgeQueue"); consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE); consumerConfig.setAsync(false); // Creating a publisher client configuration AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration( getAMQPPort(), ExchangeType.QUEUE, "oneByOneUnacknowledgeQueue"); publisherConfig.setNumberOfMessagesToSend(sendCount); // Creating clients AndesClient consumerClient = new AndesClient(consumerConfig, true); final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0); MessageConsumer receiver = andesJMSConsumer.getReceiver(); receiver.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; if (Integer.parseInt(textMessage.getText().split("#")[1]) % 3 != 0 || getMessageList(receivedMessages).contains(textMessage.getText())) { message.acknowledge(); } receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance())); andesJMSConsumer.getReceivedMessageCount().incrementAndGet(); } catch (JMSException e) { throw new RuntimeException("Exception occurred when receiving messages.", e); } } }); AndesClient publisherClient = new AndesClient(publisherConfig, true); AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0); MessageProducer sender = andesJMSPublisher.getSender(); for (int i = 0; i < sendCount; i++) { TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i)); sender.send(textMessage); } AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME); log.info("Received Messages : " + getMessageList(receivedMessages)); for (int i = 0; i < sendCount; i++) { Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i), "Invalid messages received. #" + Integer.toString(i) + " expected."); } validateMessageContentAndDelay(receivedMessages, 0, 10, "#0"); validateMessageContentAndDelay(receivedMessages, 1, 11, "#3"); validateMessageContentAndDelay(receivedMessages, 2, 12, "#6"); validateMessageContentAndDelay(receivedMessages, 3, 13, "#9"); Assert.assertEquals(receivedMessages.size(), sendCount + 4, "Message receiving failed."); }
From source file:org.wso2.mb.integration.tests.amqp.functional.PerMessageAcknowledgementsTestCase.java
/** * This test publishes 1000 messages and the subscriber reject each 100th message and then wait for the redelivered * message.//from w ww.j a va 2s .co m * * @throws AndesClientConfigurationException * @throws XPathExpressionException * @throws IOException * @throws JMSException * @throws AndesClientException * @throws NamingException */ @Test(groups = { "wso2.mb", "queue" }) public void oneByOneUnacknowledgeMessageListenerForMultipleMessagesPerAckTestCase() throws AndesClientConfigurationException, XPathExpressionException, IOException, JMSException, AndesClientException, NamingException { long sendCount = 1000; final List<String> receivedMessages = new ArrayList<>(); // Creating a consumer client configuration AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(), ExchangeType.QUEUE, "oneByOneUnacknowledgeQueuePerAckMultiple"); consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE); consumerConfig.setAsync(false); // Creating a publisher client configuration AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration( getAMQPPort(), ExchangeType.QUEUE, "oneByOneUnacknowledgeQueuePerAckMultiple"); publisherConfig.setNumberOfMessagesToSend(sendCount); // Creating clients AndesClient consumerClient = new AndesClient(consumerConfig, true); final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0); MessageConsumer receiver = andesJMSConsumer.getReceiver(); receiver.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; if (Integer.parseInt(textMessage.getText().split("#")[1]) % 100 != 0 || receivedMessages.contains(textMessage.getText())) { message.acknowledge(); } receivedMessages.add(textMessage.getText()); andesJMSConsumer.getReceivedMessageCount().incrementAndGet(); } catch (JMSException e) { throw new RuntimeException("Exception occurred when receiving messages.", e); } } }); AndesClient publisherClient = new AndesClient(publisherConfig, true); MessageProducer sender = publisherClient.getPublishers().get(0).getSender(); for (int i = 0; i < sendCount; i++) { TextMessage textMessage = publisherClient.getPublishers().get(0).getSession() .createTextMessage("#" + Integer.toString(i)); sender.send(textMessage); } AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME * 2); log.info("Received Messages : " + receivedMessages); for (int i = 0; i < sendCount; i++) { Assert.assertEquals(receivedMessages.get(i), "#" + Integer.toString(i), "Invalid messages received. #" + Integer.toString(i) + " expected."); } Assert.assertEquals(receivedMessages.get(1000), "#0", "Invalid messages received."); Assert.assertEquals(receivedMessages.get(1001), "#100", "Invalid messages received."); Assert.assertEquals(receivedMessages.get(1002), "#200", "Invalid messages received."); Assert.assertEquals(receivedMessages.get(1003), "#300", "Invalid messages received."); Assert.assertEquals(receivedMessages.get(1004), "#400", "Invalid messages received."); Assert.assertEquals(receivedMessages.get(1005), "#500", "Invalid messages received."); Assert.assertEquals(receivedMessages.get(1006), "#600", "Invalid messages received."); Assert.assertEquals(receivedMessages.get(1007), "#700", "Invalid messages received."); Assert.assertEquals(receivedMessages.get(1008), "#800", "Invalid messages received."); Assert.assertEquals(receivedMessages.get(1009), "#900", "Invalid messages received."); Assert.assertEquals(receivedMessages.size(), sendCount + 10, "Message receiving failed."); }
From source file:org.wso2.mb.integration.tests.amqp.functional.PerMessageAcknowledgementsTestCase.java
/** * This test publishes 1000 messages and the subscriber accepts all message and then wait for the redelivered * message./* ww w . java2s . com*/ * * @throws AndesClientConfigurationException * @throws XPathExpressionException * @throws IOException * @throws JMSException * @throws AndesClientException * @throws NamingException */ @Test(groups = { "wso2.mb", "queue" }) public void allAcknowledgeMessageListenerForMultipleMessagesTestCase() throws AndesClientConfigurationException, XPathExpressionException, IOException, JMSException, AndesClientException, NamingException { long sendCount = 1000; final List<String> receivedMessages = new ArrayList<>(); // Creating a consumer client configuration AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(), ExchangeType.QUEUE, "allAcknowledgeMultiplePerAckQueue"); consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE); consumerConfig.setAsync(false); // Creating a publisher client configuration AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration( getAMQPPort(), ExchangeType.QUEUE, "allAcknowledgeMultiplePerAckQueue"); publisherConfig.setNumberOfMessagesToSend(sendCount); // Creating clients AndesClient consumerClient = new AndesClient(consumerConfig, true); final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0); MessageConsumer receiver = andesJMSConsumer.getReceiver(); receiver.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; message.acknowledge(); receivedMessages.add(textMessage.getText()); andesJMSConsumer.getReceivedMessageCount().incrementAndGet(); } catch (JMSException e) { throw new RuntimeException("Exception occurred when receiving messages.", e); } } }); AndesClient publisherClient = new AndesClient(publisherConfig, true); MessageProducer sender = publisherClient.getPublishers().get(0).getSender(); for (int i = 0; i < sendCount; i++) { TextMessage textMessage = publisherClient.getPublishers().get(0).getSession() .createTextMessage("#" + Integer.toString(i)); sender.send(textMessage); } AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME * 2); log.info("Received Messages : " + receivedMessages); for (int i = 0; i < sendCount; i++) { Assert.assertEquals(receivedMessages.get(i), "#" + Integer.toString(i), "Invalid messages received. #" + Integer.toString(i) + " expected."); } Assert.assertEquals(receivedMessages.size(), sendCount, "Message receiving failed."); }
From source file:org.apache.servicemix.jms.endpoints.JmsProviderEndpoint.java
/** * Create the message listener container to receive response messages. * // ww w .jav a 2 s . co m * @return */ protected AbstractMessageListenerContainer createListenerContainer() { DefaultMessageListenerContainer cont; if (isJms102()) { cont = new DefaultMessageListenerContainer102(); } else { cont = new DefaultMessageListenerContainer(); } cont.setConnectionFactory(getConnectionFactory()); cont.setDestination(getReplyDestination()); cont.setPubSubDomain(isPubSubDomain()); cont.setPubSubNoLocal(isPubSubNoLocal()); cont.setMessageListener(new MessageListener() { public void onMessage(Message message) { JmsProviderEndpoint.this.onMessage(message); } }); cont.setAutoStartup(false); cont.afterPropertiesSet(); return cont; }
From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java
/** * This test publishes 10 messages and the subscriber rejects first 4 messages and then wait for the redelivered * message.//from www . j a v a 2s. c om * <p/> * The redelivered message is tested against the same message content with the original message and the timestamps * are also checked against the original message timestamp to make sure that the message was delayed. * * @throws AndesClientConfigurationException * @throws XPathExpressionException * @throws IOException * @throws JMSException * @throws AndesClientException * @throws NamingException */ @Test(groups = { "wso2.mb", "queue" }) public void firstFewUnacknowledgeMessageListenerTestCase() throws AndesClientConfigurationException, XPathExpressionException, IOException, JMSException, AndesClientException, NamingException { long sendCount = 10; final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>(); // Creating a consumer client configuration AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(), ExchangeType.QUEUE, "firstFewUnacknowledgeQueue"); consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE); consumerConfig.setAsync(false); // Creating a publisher client configuration AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration( getAMQPPort(), ExchangeType.QUEUE, "firstFewUnacknowledgeQueue"); publisherConfig.setNumberOfMessagesToSend(sendCount); // Creating clients AndesClient consumerClient = new AndesClient(consumerConfig, true); final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0); MessageConsumer receiver = andesJMSConsumer.getReceiver(); receiver.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; if (Integer.parseInt(textMessage.getText().split("#")[1]) >= 4 || getMessageList(receivedMessages).contains(textMessage.getText())) { message.acknowledge(); } receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance())); andesJMSConsumer.getReceivedMessageCount().incrementAndGet(); } catch (JMSException e) { throw new RuntimeException("Exception occurred when receiving messages.", e); } } }); AndesClient publisherClient = new AndesClient(publisherConfig, true); AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0); MessageProducer sender = andesJMSPublisher.getSender(); for (int i = 0; i < sendCount; i++) { TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i)); sender.send(textMessage); } AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME); log.info("Received Messages : " + getMessageList(receivedMessages)); for (int i = 0; i < sendCount; i++) { Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i), "Invalid messages received. #" + Integer.toString(i) + " expected."); } validateMessageContentAndDelay(receivedMessages, 0, 10, "#0"); validateMessageContentAndDelay(receivedMessages, 1, 11, "#1"); validateMessageContentAndDelay(receivedMessages, 2, 12, "#2"); validateMessageContentAndDelay(receivedMessages, 3, 13, "#3"); Assert.assertEquals(receivedMessages.size(), sendCount + 4, "Message receiving failed."); }