private void initializeJmsObjectsForQueue() throws JMSException { for (int i = 0; i < threads; i++) { Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE); if (destination == null) destination = session.createQueue(destinationName); MessageConsumer consumer = session.createConsumer(destination); IgniteJmsMessageListener messageListener = new IgniteJmsMessageListener(session, false); consumer.setMessageListener(messageListener); consumers.add(consumer); sessions.add(session); listeners.add(messageListener); } }
public JMSMessageListenerAdapter addQueueListener(String queueName, final GoMessageListener listener) { try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(session.createQueue(queueName)); return JMSMessageListenerAdapter.startListening(consumer, listener, daemonThreadStatsCollector); } catch (Exception e) { throw bomb(e); } }
public MessageSender createQueueSender(String queueName) { try { Session session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(session.createQueue(queueName)); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); return new ActiveMqMessageSender(session, producer); } catch (Exception e) { throw bomb(e); } }
@Test public void replyPayloadToQueue() throws JMSException { Session session = mock(Session.class); Queue replyDestination = mock(Queue.class); given(session.createQueue("queueOut")).willReturn(replyDestination); MessageProducer messageProducer = mock(MessageProducer.class); TextMessage responseMessage = mock(TextMessage.class); given(session.createTextMessage("Response")).willReturn(responseMessage); given(session.createProducer(replyDestination)).willReturn(messageProducer); MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadToQueue", Message.class); listener.onMessage(mock(javax.jms.Message.class), session); verify(session).createQueue("queueOut"); verify(session).createTextMessage("Response"); verify(messageProducer).send(responseMessage); verify(messageProducer).close(); }
@Test public void testCreateQueue() throws Exception { Connection conn = getConnectionFactory().createConnection(); Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = sess.createQueue("Queue1"); MessageProducer producer = sess.createProducer(queue); MessageConsumer consumer = sess.createConsumer(queue); conn.start(); Message m = sess.createTextMessage("testing"); producer.send(m); Message m2 = consumer.receive(3000); ProxyAssertSupport.assertNotNull(m2); conn.close(); }
connection.setClientID(clientId.get()); connection.start(); } catch (JMSException e) { throw new FlumeException("Could not create connection to broker", e); session = connection.createSession(true, Session.SESSION_TRANSACTED); } catch (JMSException e) { throw new FlumeException("Could not create session", e); switch (destinationType) { case QUEUE: destination = session.createQueue(destinationName); break; case TOPIC: messageSelector.isEmpty() ? null : messageSelector, true); } else { messageConsumer = session.createConsumer(destination, messageSelector.isEmpty() ? null : messageSelector);
@Before public void setUp() throws Exception { brokerService = new BrokerService(); brokerService.setPersistent(false); brokerService.start(); factory = new ActiveMQConnectionFactory(BrokerRegistry.getInstance().findFirst().getVmConnectorURI()); producerConnection = factory.createConnection(); producerConnection.start(); producerSession = producerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE); queue = producerSession.createQueue(getClass().getName()); producer = producerSession.createProducer(queue); }
public void testCloseConsumer() throws Exception { Destination dest = session.createQueue(getSubject() + "?consumer.prefetchSize=0"); producer = session.createProducer(dest); beginTx(); producer.send(session.createTextMessage("message 1")); producer.send(session.createTextMessage("message 2")); commitTx(); beginTx(); consumer = session.createConsumer(dest); Message message1 = consumer.receive(1000); String text1 = ((TextMessage)message1).getText(); assertNotNull(message1); assertEquals("message 1", text1); consumer.close(); consumer = session.createConsumer(dest); Message message2 = consumer.receive(1000); String text2 = ((TextMessage)message2).getText(); assertNotNull(message2); assertEquals("message 2", text2); commitTx(); }
protected synchronized void initSession() throws JMSException { if (producerSession == null || consumerSession == null) { producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); queue = producerSession.createQueue(getName()); producer = producerSession.createProducer(queue); consumer = consumerSession.createConsumer(queue); } }
Mockito.when(session.createQueue(Mockito.anyString())).thenReturn(queue); Mockito.when(session.createBytesMessage()).thenReturn(Mockito.mock(BytesMessage.class)); MessageProducer producer = Mockito.mock(MessageProducer.class); Mockito.when(session.createProducer(queue)).thenReturn(producer); Mockito.when(connection.createSession(Mockito.anyBoolean(), Mockito.anyInt())).thenReturn(session); Mockito.when(this.jmsConnectionFactory.createConnection()).thenReturn(connection); Mockito.verify(this.jmsMessageSender).createConnection(uri); Mockito.verify(session).createQueue(destinationName);
MQConnectionFactory factory = new MQConnectionFactory(); factory.setQueueManager("myQmgr"); factory.setTransportType(WMQConstants.WMQ_CM_BINDINGS); Connection conn = factory.createConnection(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue q = session.createQueue("myQ"); TextMessage msg = session.createTextMessage(); msg.setText("My message body"); MessageProducer sender = session.createProducer(q);
ConnectionFactory factory = getJMSConnectionFactory(); Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("MyQueue"); MessageConsumer consumer = session.createConsumer(queue); connection.start(); Message message = consumer.receive();
@Test public void replyPayloadToDestination() throws JMSException { Session session = mock(Session.class); MessageProducer messageProducer = mock(MessageProducer.class); TextMessage responseMessage = mock(TextMessage.class); given(session.createTextMessage("Response")).willReturn(responseMessage); given(session.createProducer(sharedReplyDestination)).willReturn(messageProducer); MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadToDestination", Message.class); listener.onMessage(mock(javax.jms.Message.class), session); verify(session, times(0)).createQueue(anyString()); verify(session).createTextMessage("Response"); verify(messageProducer).send(responseMessage); verify(messageProducer).close(); }
private void testObjectMessageWithArrayListPayload(Connection producerConnection, Connection consumerConnection) throws Throwable { ArrayList<String> payload = new ArrayList<>(); payload.add("aString"); Session session = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(getQueueName()); MessageProducer producer = session.createProducer(queue); ObjectMessage objectMessage = session.createObjectMessage(payload); producer.send(objectMessage); session.close(); session = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue consumerQueue = session.createQueue(getQueueName()); MessageConsumer cons = session.createConsumer(consumerQueue); consumerConnection.start(); objectMessage = (ObjectMessage) cons.receive(5000); assertNotNull(objectMessage); @SuppressWarnings("unchecked") ArrayList<String> received = (ArrayList<String>) objectMessage.getObject(); assertEquals(received.get(0), "aString"); consumerConnection.close(); }
given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); verify(connection).start();
public MdbInvocationHandler(ConnectionFactory connectionFactory, String requestQueueName) throws JMSException { // open a connection connection = connectionFactory.createConnection(); connection.start(); // create a session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // create the request queue requestQueue = session.createQueue(requestQueueName); // create a producer which is used to send requests producer = session.createProducer(requestQueue); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); }
@Test public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception { MessageConsumer messageConsumer = mock(MessageConsumer.class); Session session = mock(Session.class); // Queue gets created in order to create MessageConsumer for that Destination... given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); // and then the MessageConsumer gets created... given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... Connection connection = mock(Connection.class); // session gets created in order to register MessageListener... given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).willReturn(connection); this.container.setConnectionFactory(connectionFactory); this.container.setDestinationName(DESTINATION_NAME); this.container.setMessageListener(new TestMessageListener()); this.container.setAutoStartup(false); this.container.afterPropertiesSet(); GenericApplicationContext context = new GenericApplicationContext(); context.getBeanFactory().registerSingleton("messageListenerContainer", this.container); context.refresh(); verify(connection).setExceptionListener(this.container); }
@Test public void replyWithFullQoS() throws JMSException { Session session = mock(Session.class); Queue replyDestination = mock(Queue.class); given(session.createQueue("queueOut")).willReturn(replyDestination); MessageProducer messageProducer = mock(MessageProducer.class); TextMessage responseMessage = mock(TextMessage.class); given(session.createTextMessage("Response")).willReturn(responseMessage); given(session.createProducer(replyDestination)).willReturn(messageProducer); MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadToQueue", Message.class); QosSettings settings = new QosSettings(DeliveryMode.NON_PERSISTENT, 6, 6000); listener.setResponseQosSettings(settings); listener.onMessage(mock(javax.jms.Message.class), session); verify(session).createQueue("queueOut"); verify(session).createTextMessage("Response"); verify(messageProducer).send(responseMessage, DeliveryMode.NON_PERSISTENT, 6, 6000); verify(messageProducer).close(); }
@Test public void testAutoAck() throws Exception { Connection connection = factory.createConnection(); Collection<Session> sessions = new LinkedList<>(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(queueName); System.out.println("Queue:" + queue); MessageProducer producer = session.createProducer(queue); MessageConsumer consumer = session.createConsumer(queue); TextMessage msg = session.createTextMessage("test"); msg.setStringProperty("abc", "testAutoACK"); producer.send(msg); Assert.assertNull(consumer.receive(100)); connection.start(); TextMessage message = (TextMessage) consumer.receive(5000); Assert.assertNotNull(message); connection.close(); System.err.println("Done!!!"); }
@Test public void testDestroyClosesConsumersSessionsAndConnectionInThatOrder() throws Exception { MessageConsumer messageConsumer = mock(MessageConsumer.class); Session session = mock(Session.class); // Queue gets created in order to create MessageConsumer for that Destination... given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); // and then the MessageConsumer gets created... given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... Connection connection = mock(Connection.class); // session gets created in order to register MessageListener... given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); // and the connection is start()ed after the listener is registered... ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).willReturn(connection); this.container.setConnectionFactory(connectionFactory); this.container.setDestinationName(DESTINATION_NAME); this.container.setMessageListener(new TestMessageListener()); this.container.afterPropertiesSet(); this.container.start(); this.container.destroy(); verify(messageConsumer).close(); verify(session).close(); verify(connection).setExceptionListener(this.container); verify(connection).start(); verify(connection).close(); }