@Override // Get the jms session belonging to the thread publishing this event public void run() { MessageProducer messageProducer = null; try { Session session = sessionManager.get(); Destination destination = createDestination(session, destinationName); messageProducer = session.createProducer(destination); TextMessage textMessage = getEventMarshaller().marshallEvent(descriptor, event, session); messageProducer.send(textMessage, textMessage.getJMSDeliveryMode(), textMessage.getJMSPriority(), textMessage.getJMSExpiration()); success = true; } catch (CougarFrameworkException cfe) { // Catch possible exception thrown from session creation success = false; error = cfe; } catch (JMSException ex) { // Catch any other exception thrown during message publication success = false; error = ex; } finally { if (messageProducer != null) { try { messageProducer.close(); } catch (JMSException e) { LOGGER.warn("Failed to close message producer", e); } } unlock(); } } }
@Override public JmsRecord mapMessage(Message message) throws Exception { TextMessage textMessage = (TextMessage) message; Map<String, Object> properties = new HashMap<>(); @SuppressWarnings("rawtypes") Enumeration propertyNames = textMessage.getPropertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); properties.put(propertyName, textMessage.getObjectProperty(propertyName)); } JmsRecord jmsRecord = new JmsRecord( textMessage.getJMSMessageID(), textMessage.getJMSTimestamp(), textMessage.getJMSCorrelationID(), textMessage.getJMSReplyTo(), textMessage.getJMSDestination(), textMessage.getJMSDeliveryMode(), textMessage.getJMSRedelivered(), textMessage.getJMSType(), textMessage.getJMSExpiration(), textMessage.getJMSPriority(), properties, textMessage.getText()); return jmsRecord; } })
@Test public void testJMSExpirationOnSelector() throws Exception { Connection conn = null; try { conn = getConnectionFactory().createConnection(); conn.start(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer prod = session.createProducer(queue1); TextMessage msg1 = session.createTextMessage("msg1"); prod.send(msg1); prod.setTimeToLive(100000); TextMessage msg2 = session.createTextMessage("msg2"); prod.send(msg2); long expire = msg2.getJMSExpiration(); String selector = "JMSExpiration = " + expire; MessageConsumer cons = session.createConsumer(queue1, selector); conn.start(); TextMessage rec = (TextMessage) cons.receive(10000); assertNotNull(rec); Assert.assertEquals("msg2", rec.getText()); assertNull(cons.receiveNoWait()); } finally { if (conn != null) { conn.close(); } } }
@Test(timeout = 30000) public void testSelectorsWithJMSExpirationOnQueue() throws Exception { final Connection connection = createConnection(); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(getQueueName()); MessageProducer producer = session.createProducer(destination); TextMessage message1 = session.createTextMessage(); message1.setText("filtered"); producer.send(message1, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); TextMessage message2 = session.createTextMessage(); message2.setText("expected"); producer.send(message2, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, 60000); MessageConsumer consumer = session.createConsumer(destination, "JMSExpiration = " + message2.getJMSExpiration()); connection.start(); Message msg = consumer.receive(2000); assertNotNull(msg); assertTrue(msg instanceof TextMessage); assertEquals("Unexpected JMSExpiration value", message2.getJMSExpiration(), msg.getJMSExpiration()); assertEquals("Unexpected message content", "expected", ((TextMessage) msg).getText()); } finally { connection.close(); } }