@Test public void testOnlyOneConsumer() throws InterruptedException { try { producerOnlyOneConsumer.sendName("Legolas", "Greenleaf"); String greeting1 = consumer1OnlyOneConsumer.getGreeting(1000, true); assertEquals("Hello Legolas Greenleaf!", greeting1); Thread.sleep(1000); String greeting2 = consumer2OnlyOneConsumer.getGreeting(1000, true); assertEquals("no greeting", greeting2); } catch (JMSException e) { fail("a JMS Exception occurred"); } }
public String getGreeting(int timeout, boolean acknowledge) throws JMSException { String greeting = NO_GREETING; Message message = messageConsumer.receive(timeout); if (message != null) { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); LOGGER.debug(clientId + ": received message with text='{}'", text); if (acknowledge) { message.acknowledge(); LOGGER.debug(clientId + ": message acknowledged"); } else { LOGGER.debug(clientId + ": message not acknowledged"); } greeting = "Hello " + text + "!"; } else { LOGGER.debug(clientId + ": no message received"); } LOGGER.info("greeting={}", greeting); return greeting; }
Consumer { public String getGreeting(int timeout, boolean acknowledge) throws JMSException { String greeting = NO_GREETING; Message message = messageConsumer.receive(timeout); if (message != null) { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); LOGGER.debug(clientId + ": received message with text='{}'", text); if (acknowledge) { message.acknowledge(); LOGGER.debug(clientId + ": message acknowledged"); } else { LOGGER.debug(clientId + ": message not acknowledged"); } greeting = "Hello " + text + "!"; } else { LOGGER.debug(clientId + ": no message received"); } LOGGER.info("greeting={}", greeting); return greeting; } }
Consumer { public String getGreeting(int timeout, boolean acknowledge) throws JMSException { String greeting = NO_GREETING; Message message = messageConsumer.receive(timeout); if (message != null) { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); LOGGER.debug(clientId + ": received message with text='{}'", text); if (acknowledge) { message.acknowledge(); LOGGER.debug(clientId + ": message acknowledged"); } else { LOGGER.debug(clientId + ": message not acknowledged"); } greeting = "Hello " + text + "!"; } else { LOGGER.debug(clientId + ": no message received"); } LOGGER.info("greeting={}", greeting); return greeting; }  }
Consumer { public String getGreeting(int timeout, boolean acknowledge) throws JMSException { String greeting = NO_GREETING; Message message = messageConsumer.receive(timeout); if (message != null) { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); LOGGER.debug(clientId + ": received message with text='{}'", text); if (acknowledge) { message.acknowledge(); LOGGER.debug(clientId + ": message acknowledged"); } else { LOGGER.debug(clientId + ": message not acknowledged"); } greeting = "Hello " + text + "!"; } else { LOGGER.debug(clientId + ": no message received"); } LOGGER.info("greeting={}", greeting); return greeting; }  void create(String clientId, String queueName); void closeConnection(); String getGreeting(int timeout, boolean acknowledge); }
Consumer { public String getGreeting(int timeout, boolean acknowledge) throws JMSException { String greeting = NO_GREETING; Message message = messageConsumer.receive(timeout); if (message != null) { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); LOGGER.debug(clientId + ": received message with text='{}'", text); if (acknowledge) { message.acknowledge(); LOGGER.debug(clientId + ": message acknowledged"); } else { LOGGER.debug(clientId + ": message not acknowledged"); } greeting = "Hello " + text + "!"; } else { LOGGER.debug(clientId + ": no message received"); } LOGGER.info("greeting={}", greeting); return greeting; }  void create(String clientId, String queueName); void closeConnection(); String getGreeting(int timeout, boolean acknowledge);  }