@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 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 testCreateContextClientAckSession() throws Exception { JMSContext context = jms2ConnectionFactory.createContext(JMSContext.CLIENT_ACKNOWLEDGE); assertNotNull(context); assertEquals(Jms2Context.class, context.getClass()); }
@Override @After public void tearDown() throws Exception { try { for (JMSContext jmsContext : contextSet) { jmsContext.close(); } } catch (RuntimeException ignored) { // no-op } finally { contextSet.clear(); } try { if (conn != null) conn.close(); } catch (Exception e) { // no-op } namingContext.close(); jmsServer.stop(); server = null; cf = null; jmsServer = null; namingContext = null; MBeanServerFactory.releaseMBeanServer(mbeanServer); mbeanServer = null; ServiceUtils.setTransactionManager(null); super.tearDown(); }
@Test public void testCreateTransactedSession() throws Exception { JMSContext context = jms2ConnectionFactory.createContext(JMSContext.SESSION_TRANSACTED); assertNotNull(context); assertEquals(Jms2Context.class, context.getClass()); }
@Test public void createContext() throws Exception { JMSContext context = jms2ConnectionFactory.createContext(); assertNotNull(context); assertEquals(Jms2Context.class, context.getClass()); }
public void onMetricsServiceReady(@Observes @ServiceReady ServiceReadyEvent event) { context = connectionFactory.createContext(); gaugesConsumer = context.createConsumer(gaugesQueue); gaugesConsumer.setMessageListener( new BasicMessageListener<MetricDataMessage>() { @Override protected void onBasicMessage(MetricDataMessage basicMessage) { onGaugeData(basicMessage.getMetricData()); } }); countersConsumer = context.createConsumer(countersQueue); countersConsumer.setMessageListener( new BasicMessageListener<MetricDataMessage>() { @Override protected void onBasicMessage(MetricDataMessage basicMessage) { onCounterData(basicMessage.getMetricData()); } }); availabilityConsumer = context.createConsumer(availabilityQueue); availabilityConsumer.setMessageListener( new BasicMessageListener<AvailDataMessage>() { @Override protected void onBasicMessage(AvailDataMessage basicMessage) { onAvailData(basicMessage.getAvailData()); } }); }
@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()); }
@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 testClusteredTopic() throws Exception { InitialContext contextFromServer0 = createJNDIContextFromServer0(); InitialContext contextFromServer1 = createJNDIContextFromServer1(); try (JMSContext jmsContext0 = createJMSContext(createJNDIContextFromServer0()); JMSContext jmsContext1 = createJMSContext(createJNDIContextFromServer1())) { JMSConsumer consumer0 = jmsContext0.createConsumer((Destination) contextFromServer0.lookup(jmsTopicLookup)); JMSConsumer consumer1 = jmsContext1.createConsumer((Destination) contextFromServer1.lookup(jmsTopicLookup)); String text = UUID.randomUUID().toString(); // WIP test if the problem is that the view is not yet propagated Thread.sleep(ClusteringTestConstants.GRACE_TIME_TO_MEMBERSHIP_CHANGE); // send a message to the topic on server 0 sendMessage(contextFromServer0, jmsTopicLookup, text); // consumers receive it on both servers receiveMessage(consumer0, text); receiveMessage(consumer1, text); String anotherText = UUID.randomUUID().toString(); // send another message to topic on server 1 sendMessage(contextFromServer1, jmsTopicLookup, anotherText); // consumers receive it on both servers receiveMessage(consumer0, anotherText); receiveMessage(consumer1, anotherText); } }
@Test public void testGetClientId() { JMSContext context2 = addContext(context.createContext(Session.AUTO_ACKNOWLEDGE)); final String id = "ID: " + random.nextInt(); context.setClientID(id); Assert.assertEquals( "id's must match because the connection is shared", id, context2.getClientID()); }
@Test public void testSetGetClientIdNewContext() { final String id = "123"; JMSContext c = context; // createContext(); c.setClientID(id); JMSContext c2 = addContext(c.createContext(Session.CLIENT_ACKNOWLEDGE)); Assert.assertEquals(id, c2.getClientID()); }
@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 testCreateContextCredentialsAndClientAckSession() throws Exception { final String username = "******"; final String password = "******"; JMSContext context = jms2ConnectionFactory.createContext(username, password, JMSContext.CLIENT_ACKNOWLEDGE); assertNotNull(context); assertEquals(Jms2Context.class, context.getClass()); }
/** Envia um objeto complexo para a fila JMS */ public void enviarObjetoForma2(Aluno aluno) { try { ObjectMessage objMessage = context.createObjectMessage(); objMessage.setObject(aluno); context.createProducer().send(fila, objMessage); } catch (JMSException ex) { ex.printStackTrace(); } }
@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(); }
@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 } }
protected static void sendMessage(Context ctx, String destinationLookup, String text) throws NamingException { ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory"); assertNotNull(cf); Destination destination = (Destination) ctx.lookup(destinationLookup); assertNotNull(destination); try (JMSContext context = cf.createContext("guest", "guest")) { context.createProducer().send(destination, text); } }
@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 } }
public void receiveAll() { System.out.println("--> Receiving redundant messages ..."); try { QueueBrowser browser = context.createBrowser(myQueue); while (browser.getEnumeration().hasMoreElements()) { System.out.println("--> here is one"); context.createConsumer(myQueue).receiveBody(String.class, 1000); } } catch (JMSException ex) { Logger.getLogger(MessageReceiverSync.class.getName()).log(Level.SEVERE, null, ex); } }
protected static void receiveMessage(Context ctx, String destinationLookup, String expectedText) throws NamingException { ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory"); assertNotNull(cf); Destination destination = (Destination) ctx.lookup(destinationLookup); assertNotNull(destination); try (JMSContext context = cf.createContext("guest", "guest"); ) { JMSConsumer consumer = context.createConsumer(destination); receiveMessage(consumer, expectedText); } }
@Test public void testSecurityOnJMSContext() throws Exception { server.getSecurityManager().addUser("IDo", "Exist"); try { JMSContext ctx = cf.createContext("Idont", "exist"); ctx.close(); } catch (JMSSecurityRuntimeException e) { // expected } JMSContext ctx = cf.createContext("IDo", "Exist"); ctx.close(); ; }
public static void main(String[] args) throws NamingException { final InitialContext ic = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) ic.lookup("jms/RemoteConnectionFactory"); Queue queue = (Queue) ic.lookup("jms/FILA.GERADOR"); try (JMSContext context = factory.createContext("jms", "jms2")) { JMSConsumer consumer = context.createConsumer(queue); consumer.setMessageListener(new TratadorDeMensagem()); context.start(); Scanner teclado = new Scanner(System.in); System.out.println("Tratador esperando as mensagens na fila JMS"); teclado.nextLine(); teclado.close(); context.stop(); } }
@Test public void testCreateQueueConnection() throws Exception { server.getSecurityManager().addUser("IDo", "Exist"); try { QueueConnection queueC = ((QueueConnectionFactory) cf).createQueueConnection("IDont", "Exist"); fail("supposed to throw exception"); queueC.close(); } catch (JMSSecurityException e) { // expected } JMSContext ctx = cf.createContext("IDo", "Exist"); ctx.close(); ; }
public MapMessage convert(BusinessObject bo) throws Exception { MapMessage mm = context.createMapMessage(); for (String key : bo.keySet()) { mm.setObject(key, bo.get(key)); } return mm; }
private void closeContext() { try { context.close(); } catch (Throwable t) { error = t; } }
private void stopContext() { try { context.stop(); } catch (Throwable t) { error = t; } }
public String receiveMessage() { try (JMSConsumer consumer = context.createConsumer(pointsQueue)) { String message = consumer.receiveBody(String.class); Logger.getLogger(ReceivePointsBean.class.getName()) .log(Level.INFO, "Received message: " + message); return message; } }
@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 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()); }