MessageProducer producer = null; try { queueConnection = connectionFactory.createConnection(); queueSession = queueConnection.createSession(transacted, Session.AUTO_ACKNOWLEDGE); TextMessage message = queueSession.createTextMessage(eventXml); message.setStringProperty("LogType", "Task"); producer = queueSession.createProducer(queue); producer.setPriority(priority); producer.send(message); } catch (Exception e) { throw new RuntimeException("Error when sending JMS message with working memory event", e); if (producer != null) { try { producer.close(); } catch (JMSException e) { logger.warn("Error when closing producer", e); queueSession.close(); } catch (JMSException e) { logger.warn("Error when closing queue session", e); queueConnection.close(); } catch (JMSException e) { logger.warn("Error when closing queue connection", e);
@Override public JMSContext createContext(String userName, String password) { return obtainTargetConnectionFactory().createContext(userName, password); }
@Test public void testCreateProducerOnNullQueue() throws Exception { Connection conn = getConnectionFactory().createConnection(); Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Message m = sess.createTextMessage("something"); MessageProducer p = sess.createProducer(null); p.send(queue1, m); MessageConsumer c = sess.createConsumer(queue1); conn.start(); // receiveNoWait is not guaranteed to return message immediately TextMessage rm = (TextMessage) c.receive(1000); ProxyAssertSupport.assertEquals("something", rm.getText()); conn.close(); }
private static void receiveMessage() throws Exception { // 创建 ActiveMQ 链接,设置 Broker URL ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // 创造 JMS 链接 Connection connection = connectionFactory.createConnection(); // 启动连接 connection.start(); // 创建会话 Session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建消息目的 - Queue 名称为 "TEST" Destination destination = session.createQueue("TEST"); // 创建消息消费者 MessageConsumer messageConsumer = session.createConsumer(destination); // 获取消息 Message message = messageConsumer.receive(100); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; System.out.println("消息消费内容:" + textMessage.getText()); } // 关闭消息消费者 messageConsumer.close(); // 关闭会话 session.close(); // 关闭连接 connection.stop(); connection.close(); }
@Test public void testCreateQueueTempQueue() throws Exception { conn = cf.createConnection(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue tempQueue = session.createTemporaryQueue(); String tempQueueName = tempQueue.getQueueName(); // assertFalse(tempQueueName.startsWith(ActiveMQDestination.JMS_QUEUE_ADDRESS_PREFIX)); Queue replyQueue = session.createQueue(tempQueueName); MessageProducer producer = session.createProducer(replyQueue); producer.send(session.createMessage()); MessageConsumer consumer = session.createConsumer(replyQueue); conn.start(); assertNotNull(consumer.receive(10000)); }
given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... given(session.getTransacted()).willReturn(false); given(session.getAcknowledgeMode()).willReturn(Session.AUTO_ACKNOWLEDGE); given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); given(connectionFactory.createConnection()).willReturn(connection); verify(connection).setExceptionListener(this.container); verify(connection).start();
private static void sendMessage() throws Exception { // 创建 ActiveMQ 链接,设置 Broker URL ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // 创造 JMS 链接 Connection connection = connectionFactory.createConnection(); // 创建会话 Session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建消息目的 - Queue 名称为 "TEST" Destination destination = session.createQueue("TEST"); // 创建消息生产者 MessageProducer producer = session.createProducer(destination); // 创建消息 - 文本消息 ActiveMQTextMessage message = new ActiveMQTextMessage(); message.setText("Hello,World"); // 发送文本消息 producer.send(message); // 关闭消息生产者 producer.close(); // 关闭会话 session.close(); // 关闭连接 connection.close(); } }
public List<Message> receive(Queue queue) throws Exception { List<Message> messages = new ArrayList<Message>(); Connection qconnetion = factory.createConnection(); Session qsession = qconnetion.createSession(true, QueueSession.AUTO_ACKNOWLEDGE); MessageConsumer consumer = qsession.createConsumer(queue); qconnetion.start(); Message m = null; while ((m = consumer.receiveNoWait()) != null) { messages.add(m); } consumer.close(); qsession.close(); qconnetion.close(); return messages; } }
void receiveAndProcess(Queue queue, EntityManagerFactory entityManagerFactory, long waitTime, int countDown) throws Exception { Connection qconnetion = factory.createConnection(); Session qsession = qconnetion.createSession(true, QueueSession.AUTO_ACKNOWLEDGE); MessageConsumer consumer = qsession.createConsumer(queue); qconnetion.start(); consumer.setMessageListener(rec); Assertions.assertThat(latch.await(waitTime, TimeUnit.MILLISECONDS)).isTrue(); consumer.close(); qsession.close(); qconnetion.close();
@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(); }
@Test public void testTransactionCommitWithMessageProducer() throws JMSException { Destination dest = new StubQueue(); ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); Session session = mock(Session.class); MessageProducer producer = mock(MessageProducer.class); final Message message = mock(Message.class); given(cf.createConnection()).willReturn(con); given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session); given(session.createProducer(dest)).willReturn(producer); given(session.getTransacted()).willReturn(true); JmsTransactionManager tm = new JmsTransactionManager(cf); TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); JmsTemplate jt = new JmsTemplate(cf); jt.send(dest, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return message; } }); tm.commit(ts); verify(producer).send(message); verify(session).commit(); verify(producer).close(); verify(session).close(); verify(con).close(); }
public void sendMessage(SimpleString queue) throws Exception { ConnectionFactory fact = getCF(); Connection connection = fact.createConnection(); try { Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); connection.start(); Destination destination = session.createQueue(queue.toString()); MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage(); message.setText("Message"); producer.send(message); } finally { connection.close(); } }
@Test public void testAutoCreateOnDurableSubscribeToTopic() throws Exception { Connection connection = cf.createConnection(); connection.setClientID("myClientID"); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); javax.jms.Topic topic = ActiveMQJMSClient.createTopic(QUEUE_NAME); MessageConsumer consumer = session.createDurableConsumer(topic, "myDurableSub"); MessageProducer producer = session.createProducer(topic); producer.send(session.createTextMessage("msg")); connection.start(); assertNotNull(consumer.receive(500)); connection.close(); assertNotNull(server.getManagementService().getResource(ResourceNames.ADDRESS + "test")); assertNotNull(server.locateQueue(SimpleString.toSimpleString("myClientID.myDurableSub"))); }
private void checkDestination(String name) throws Exception { ConnectionFactory cf = (ConnectionFactory) namingContext.lookup("/someCF"); Connection conn = cf.createConnection(); Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination dest = (Destination) namingContext.lookup(name); conn.start(); MessageConsumer cons = sess.createConsumer(dest); MessageProducer prod = sess.createProducer(dest); prod.send(sess.createMessage()); assertNotNull(cons.receiveNoWait()); conn.close(); }
Session nonTxSession = mock(Session.class); given(cf.createConnection()).willReturn(con); given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession); given(txSession.getTransacted()).willReturn(true); given(con.createSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted(); session1.close(); // should lead to rollback session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close();
ConnectionFactory cf = jmsProvider.connectionFactory(); Destination dest = jmsProvider.destination(); this.connection = cf.createConnection(); this.session = connection.createSession(false, jmsAcknowledgeMode); MessageConsumer consumer = session.createConsumer(dest); consumer.setMessageListener(this); this.connection.start();
@Test public void testSharedDurableConsumer() throws Exception { conn = cf.createConnection(); conn.start(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); topic = ActiveMQJMSClient.createTopic(T_NAME); MessageConsumer cons = session.createSharedDurableConsumer(topic, "test1"); MessageProducer producer = session.createProducer(topic); producer.send(session.createTextMessage("test")); TextMessage txt = (TextMessage) cons.receive(5000); Assert.assertNotNull(txt); }
@Test public void testSharedConsumer() throws Exception { conn = cf.createConnection(); conn.start(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); topic = ActiveMQJMSClient.createTopic(T_NAME); MessageConsumer cons = session.createSharedConsumer(topic, "test1"); MessageProducer producer = session.createProducer(topic); producer.send(session.createTextMessage("test")); TextMessage txt = (TextMessage) cons.receive(5000); Assert.assertNotNull(txt); }
@Override public Object execute() throws Exception { Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queue); MessageProducer producer = session.createProducer(destination); TextMessage textMessage = session.createTextMessage(message); producer.send(textMessage); return null; }
private void receiveJMS(int nMsgs, ConnectionFactory factory) throws JMSException { Connection connection2 = factory.createConnection(); Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE); connection2.start(); MessageConsumer consumer = session2.createConsumer(session2.createQueue(testQueueName)); for (int i = 0; i < nMsgs; i++) { Message message = consumer.receive(5000); Assert.assertNotNull(message); Assert.assertEquals(i, message.getIntProperty("i")); } connection2.close(); }