@Test public void testSendStreamMessage() throws JMSException, InterruptedException { JmsProducerCompletionListenerTest.CountingCompletionListener cl = new JmsProducerCompletionListenerTest.CountingCompletionListener(1); JMSProducer producer = context.createProducer(); producer.setAsync(cl); StreamMessage msg = context.createStreamMessage(); msg.setStringProperty("name", name.getMethodName()); String bprop = "booleanProp"; String iprop = "intProp"; msg.setBooleanProperty(bprop, true); msg.setIntProperty(iprop, 42); msg.writeBoolean(true); msg.writeInt(67); producer.send(queue1, msg); JMSConsumer consumer = context.createConsumer(queue1); Message msg2 = consumer.receive(100); Assert.assertNotNull(msg2); Assert.assertTrue(cl.completionLatch.await(1, TimeUnit.SECONDS)); StreamMessage sm = (StreamMessage) cl.lastMessage; Assert.assertEquals(true, sm.getBooleanProperty(bprop)); Assert.assertEquals(42, sm.getIntProperty(iprop)); Assert.assertEquals(true, sm.readBoolean()); Assert.assertEquals(67, sm.readInt()); }
@Test public void testDelay() throws Exception { JMSProducer producer = context.createProducer(); JMSConsumer consumer = context.createConsumer(queue1); producer.setDeliveryDelay(500); long timeStart = System.currentTimeMillis(); String strRandom = newXID().toString(); producer.send(queue1, context.createTextMessage(strRandom)); TextMessage msg = (TextMessage) consumer.receive(2500); assertNotNull(msg); long actualDelay = System.currentTimeMillis() - timeStart; assertTrue( "delay is not working, actualDelay=" + actualDelay, actualDelay >= 500 && actualDelay < 2000); assertEquals(strRandom, msg.getText()); }
@Test public void sendToNonExistantDestination() throws Exception { Destination destination = HornetQJMSClient.createQueue("DoesNotExist"); TransportConfiguration transportConfiguration = new TransportConfiguration(InVMConnectorFactory.class.getName()); ConnectionFactory localConnectionFactory = HornetQJMSClient.createConnectionFactoryWithoutHA( JMSFactoryType.CF, transportConfiguration); // Using JMS 1 API Connection connection = localConnectionFactory.createConnection(); Session session = connection.createSession(); try { MessageProducer messageProducer = session.createProducer(null); messageProducer.send(destination, session.createMessage()); Assert.fail("Succeeded in sending message to a non-existant destination using JMS 1 API!"); } catch (JMSException e) { // Expected } } // Using JMS 2 API JMSContext context = localConnectionFactory.createContext(); JMSProducer jmsProducer = context.createProducer().setDeliveryMode(DeliveryMode.PERSISTENT); try { jmsProducer.send(destination, context.createMessage()); Assert.fail("Succeeded in sending message to a non-existant destination using JMS 2 API!"); } catch (JMSRuntimeException e) { // Expected } } }
@Override public int doStartTag() throws JspException { String context = ""; try { System.out.println("jsp tag start..."); ut.begin(); JMSProducer producer = jmsContext.createProducer(); TextMessage msg = jmsContext.createTextMessage("Hello JSP Tag"); producer.send(queue, msg); context = jmsContext.toString(); ut.commit(); if (context.indexOf(transactionScope) == -1) { throw new JspException("NOT in transaction scope!"); } // Get the writer object for output. JspWriter out = pageContext.getOut(); // Perform substr operation on string. out.println(text); } catch (Exception e) { throw new JspException(e); } return SKIP_BODY; }
@Test public void testRollbackTest() { JMSContext ctx = addContext(cf.createContext(JMSContext.SESSION_TRANSACTED)); JMSProducer producer = ctx.createProducer(); JMSConsumer cons = ctx.createConsumer(queue1); producer.send(queue1, context.createTextMessage("hello")); ctx.rollback(); assertNull(cons.receiveNoWait()); producer.send(queue1, context.createTextMessage("hello")); ctx.commit(); assertNotNull(cons.receiveNoWait()); ctx.commit(); ctx.rollback(); assertNull(cons.receiveNoWait()); cons.close(); }
@Override public void sendMessage(String text) { try { JMSProducer producer = jmsContext.createProducer(); TextMessage message = jmsContext.createTextMessage(text); producer.send(testQueue, message); } catch (Exception e) { throw new EJBException(e); } }
@Test public void testInvalidMessage() { JMSProducer producer = context.createProducer(); try { producer.send(queue1, (Message) null); Assert.fail("null msg"); } catch (MessageFormatRuntimeException expected) { // no-op } }
@Test public void testInvalidDestination() { JMSProducer producer = context.createProducer(); Message msg = context.createMessage(); try { producer.send((Destination) null, msg); Assert.fail("null Destination"); } catch (InvalidDestinationRuntimeException expected) { // no-op } }
@Test public void testSetClientIdLate() { JMSProducer producer = context.createProducer(); Message msg = context.createMessage(); producer.send(queue1, msg); try { context.setClientID("id"); Assert.fail("expected exception"); } catch (IllegalStateRuntimeException e) { // no op } }
@Test public void recoverAckTest() throws Exception { // Create JMSContext with CLIENT_ACKNOWLEDGE try (JMSContext context = cf.createContext(JMSContext.CLIENT_ACKNOWLEDGE)) { int numMessages = 10; TextMessage textMessage = null; // Create JMSConsumer from JMSContext JMSConsumer consumer = context.createConsumer(queue1); // Create JMSProducer from JMSContext JMSProducer producer = context.createProducer(); // send messages for (int i = 0; i < numMessages; i++) { String message = "text message " + i; textMessage = context.createTextMessage(message); textMessage.setStringProperty("COM_SUN_JMS_TESTNAME", "recoverAckTest" + i); producer.send(queue1, textMessage); } // receive messages but do not acknowledge for (int i = 0; i < numMessages; i++) { textMessage = (TextMessage) consumer.receive(5000); assertNotNull(textMessage); } context.recover(); // receive messages a second time followed by acknowledge for (int i = 0; i < numMessages; i++) { textMessage = (TextMessage) consumer.receive(5000); assertNotNull(textMessage); } // Acknowledge all messages context.acknowledge(); } // doing this check with another context / consumer to make sure it was acked. try (JMSContext context = cf.createContext(JMSContext.CLIENT_ACKNOWLEDGE)) { // Create JMSConsumer from JMSContext JMSConsumer consumer = context.createConsumer(queue1); assertNull(consumer.receiveNoWait()); } }
@Test public void testContextStopAndCloseFromMessageListeners() throws Exception { final JMSContext context1 = context.createContext(Session.AUTO_ACKNOWLEDGE); JMSConsumer consumer1 = context1.createConsumer(queue1); final CountDownLatch latch1 = new CountDownLatch(1); InvalidMessageListener listener1 = new InvalidMessageListener(context1, latch1, 1); consumer1.setMessageListener(listener1); JMSProducer producer = context1.createProducer(); Message msg = context1.createTextMessage("first message"); producer.send(queue1, msg); latch1.await(); Throwable error1 = listener1.getError(); assertNotNull(error1); assertTrue(error1 instanceof IllegalStateRuntimeException); context1.close(); final JMSContext context2 = context.createContext(Session.AUTO_ACKNOWLEDGE); JMSConsumer consumer2 = context2.createConsumer(queue1); final CountDownLatch latch2 = new CountDownLatch(1); InvalidMessageListener listener2 = new InvalidMessageListener(context2, latch2, 2); consumer2.setMessageListener(listener2); JMSProducer producer2 = context2.createProducer(); Message msg2 = context2.createTextMessage("second message"); producer2.send(queue1, msg2); latch2.await(); Throwable error2 = listener2.getError(); assertNotNull(error2); assertTrue(error2 instanceof IllegalStateRuntimeException); context2.close(); }
@Test public void testReceiveText() throws Exception { JMSProducer producer = context.createProducer(); JMSConsumer consumer = context.createConsumer(queue1); String randomStr = newXID().toString(); System.out.println("RandomStr:" + randomStr); TextMessage sendMsg = context.createTextMessage(randomStr); producer.send(queue1, sendMsg); TextMessage receiveMsg = (TextMessage) consumer.receiveNoWait(); assertEquals(randomStr, receiveMsg.getText()); }
@Override public void onMessage(Message message) { JMSProducer producer = context.createProducer(); try { // What we need to do is find classification entries that have not been // filled out. When we find them, we send a message to *another* queue, // saying "run this classification" - with an expiry of 45 seconds. List<Classification> classifications = classificationService.findUnclassifiedEntries(); for (Classification classification : classifications) { ObjectMessage classificationMessage = context.createObjectMessage(classification); classificationMessage.setJMSExpiration(45 * 1000); producer.send(classificationQueue, classificationMessage); } } catch (JMSException jmsException) { jmsException.printStackTrace(); } }
@Test public void testDeliveryMode() throws Exception { JMSProducer producer = context.createProducer(); JMSConsumer consumer = context.createConsumer(queue1); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); String strRandom = newXID().toString(); producer.send(queue1, context.createTextMessage(strRandom)); TextMessage msg = (TextMessage) consumer.receiveNoWait(); assertNotNull(msg); assertEquals(DeliveryMode.NON_PERSISTENT, msg.getJMSDeliveryMode()); }
@Test public void testExpire() throws Exception { JMSProducer producer = context.createProducer(); producer.setTimeToLive(500); String strRandom = newXID().toString(); producer.send(queue1, context.createTextMessage(strRandom)); Thread.sleep(700); // Create consumer after message is expired, making it to expire at the server's JMSConsumer consumer = context.createConsumer(queue1); TextMessage msg = (TextMessage) consumer.receiveNoWait(); // Time to live kicked in, so it's supposed to return null assertNull(msg); strRandom = newXID().toString(); producer.send(queue1, context.createTextMessage(strRandom)); Thread.sleep(700); // Receive second message, expiring on client msg = (TextMessage) consumer.receiveNoWait(); assertNull(msg); strRandom = newXID().toString(); producer.send(queue1, context.createTextMessage(strRandom)); // will receive a message that's not expired now msg = (TextMessage) consumer.receiveNoWait(); assertNotNull(msg); assertEquals(strRandom, msg.getText()); }
@Test public void bytesMessage() throws Exception { context = cf.createContext(); try { JMSProducer producer = context.createProducer(); BytesMessage bMsg = context.createBytesMessage(); bMsg.setStringProperty("COM_SUN_JMS_TESTNAME", "sendAndRecvMsgOfEachTypeCLTest"); bMsg.writeByte((byte) 1); bMsg.writeInt((int) 22); CountDownLatch latch = new CountDownLatch(1); SimpleCompletionListener listener = new SimpleCompletionListener(latch); producer.setAsync(listener); producer.send(queue1, bMsg); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertEquals(listener.message.readByte(), (byte) 1); assertEquals(listener.message.readInt(), (int) 22); } finally { context.close(); } }
@Test public void testCloseSecondContextConnectionRemainsOpen() throws JMSException { JMSContext localContext = context.createContext(JMSContext.CLIENT_ACKNOWLEDGE); Assert.assertEquals("client_ack", JMSContext.CLIENT_ACKNOWLEDGE, localContext.getSessionMode()); JMSProducer producer = localContext.createProducer(); JMSConsumer consumer = localContext.createConsumer(queue1); final int pass = 1; for (int idx = 0; idx < 2; idx++) { Message m = localContext.createMessage(); int intProperty = random.nextInt(); m.setIntProperty("random", intProperty); Assert.assertNotNull(m); producer.send(queue1, m); m = null; Message msg = consumer.receive(100); Assert.assertNotNull("must have a msg", msg); Assert.assertEquals(intProperty, msg.getIntProperty("random")); /* In the second pass we close the connection before ack'ing */ if (idx == pass) { localContext.close(); } /** * From {@code JMSContext.close()}'s javadoc:<br> * Invoking the {@code acknowledge} method of a received message from a closed connection's * session must throw an {@code IllegalStateRuntimeException}. Closing a closed connection * must NOT throw an exception. */ try { msg.acknowledge(); Assert.assertEquals("connection should be open on pass 0. It is " + pass, 0, idx); } // HORNETQ-1209 "JMS 2.0" XXX JMSContext javadoc says we must expect a // IllegalStateRuntimeException here. But Message.ack...() says it must throws the // non-runtime variant. catch (javax.jms.IllegalStateException expected) { Assert.assertEquals("we only close the connection on pass " + pass, pass, idx); } } }
@Test public void testCreateConsumerWithSelector() throws JMSException { final String filterName = "magicIndexMessage"; final int total = 5; JMSProducer producer = context.createProducer(); JMSConsumer consumerNoSelect = context.createConsumer(queue1); JMSConsumer consumer = context.createConsumer(queue1, filterName + "=TRUE"); for (int i = 0; i < total; i++) { Message msg = context.createTextMessage("message " + i); msg.setBooleanProperty(filterName, i == 3); producer.send(queue1, msg); } Message msg0 = consumer.receive(500); Assert.assertNotNull(msg0); msg0.acknowledge(); Assert.assertNull("no more messages", consumer.receiveNoWait()); for (int i = 0; i < total - 1; i++) { Message msg = consumerNoSelect.receive(100); Assert.assertNotNull(msg); msg.acknowledge(); } Assert.assertNull("no more messages", consumerNoSelect.receiveNoWait()); }
@Test public void testReceiveBytes() throws Exception { JMSProducer producer = context.createProducer(); JMSConsumer consumer = context.createConsumer(queue1); BytesMessage bytesSend = context.createBytesMessage(); bytesSend.writeByte((byte) 1); bytesSend.writeLong(2l); producer.send(queue1, bytesSend); BytesMessage msgReceived = (BytesMessage) consumer.receiveNoWait(); byte[] bytesArray = msgReceived.getBody(byte[].class); assertEquals((byte) 1, msgReceived.readByte()); assertEquals(2l, msgReceived.readLong()); DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(bytesArray)); assertEquals((byte) 1, dataInputStream.readByte()); assertEquals(2l, dataInputStream.readLong()); }
protected final void sendMessages( JMSContext context, JMSProducer producer, Queue queue, final int total) { try { for (int j = 0; j < total; j++) { StringBuilder sb = new StringBuilder(); for (int m = 0; m < 200; m++) { sb.append(random.nextLong()); } Message msg = context.createTextMessage(sb.toString()); msg.setIntProperty("counter", j); producer.send(queue, msg); } } catch (JMSException cause) { throw new JMSRuntimeException(cause.getMessage(), cause.getErrorCode(), cause); } }