@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 } } }
@Test public void testGetAnotherContextFromIt() { JMSContext c2 = context.createContext(Session.DUPS_OK_ACKNOWLEDGE); Assert.assertNotNull(c2); Assert.assertEquals(Session.DUPS_OK_ACKNOWLEDGE, c2.getSessionMode()); Message m2 = c2.createMessage(); Assert.assertNotNull(m2); c2.close(); // should close its session, but not its (shared) connection try { c2.createMessage(); Assert.fail("session should be closed..."); } catch (JMSRuntimeException expected) { // expected } Message m1 = context.createMessage(); Assert.assertNotNull("connection must be open", m1); }
@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 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); } } }
public static void main(String[] args) { MapMessage outMessage; JMSConsumer orderConfirmReceiver; MapMessage inMessage; if (args.length != 1) { System.out.println("Error: Program takes numerical argument."); System.exit(1); } quantity = (new Integer(args[0])).intValue(); System.out.println("Retailer: Quantity to be ordered is " + quantity); /* * Create non-transacted context and sender for * vendor order queue. * Create message to vendor, setting item and * quantity values. * Send message. * Create receiver for retailer confirmation queue. * Get message and report result. * Send an end-of-message-stream message so vendor * will stop processing orders. */ try (JMSContext context = connectionFactory.createContext(); ) { outMessage = context.createMapMessage(); outMessage.setString("Item", "Computer(s)"); outMessage.setInt("Quantity", quantity); outMessage.setJMSReplyTo(retailerConfirmQueue); context.createProducer().send(vendorOrderQueue, outMessage); System.out.println("Retailer: Ordered " + quantity + " computer(s)"); orderConfirmReceiver = context.createConsumer(retailerConfirmQueue); inMessage = (MapMessage) orderConfirmReceiver.receive(); if (inMessage.getBoolean("OrderAccepted") == true) { System.out.println("Retailer: Order filled"); } else { System.out.println("Retailer: Order not filled"); } System.out.println("Retailer: Placing another order"); outMessage.setInt("Quantity", quantity * 2); context.createProducer().send(vendorOrderQueue, outMessage); System.out.println("Retailer: Ordered " + outMessage.getInt("Quantity") + " computer(s)"); inMessage = (MapMessage) orderConfirmReceiver.receive(); if (inMessage.getBoolean("OrderAccepted") == true) { System.out.println("Retailer: Order filled"); } else { System.out.println("Retailer: Order not filled"); } /* * Send a non-text control message indicating end * of messages. */ context.createProducer().send(vendorOrderQueue, context.createMessage()); } catch (JMSException e) { System.err.println("Retailer: Exception occurred: " + e.toString()); } }