private void sendMessage(MessageProducer producer, String threadName) throws Exception { Message message = createMessage(sentCount.get()); producer.send(message); if (LOG.isDebugEnabled()) { LOG.debug(threadName + " Sent: " + (message instanceof TextMessage ? ((TextMessage) message).getText() : message.getJMSMessageID())); } if (transactionBatchSize > 0 && sentCount.get() > 0 && sentCount.get() % transactionBatchSize == 0) { LOG.info(threadName + " Committing transaction: " + transactions++); session.commit(); } if (sleep > 0) { Thread.sleep(sleep); } }
consumer = session.createDurableSubscriber((Topic) destination, getName()); } else { consumer = session.createConsumer(destination); Message msg = consumer.receive(receiveTimeOut); if (msg != null) { LOG.info(threadName + " Received " + (msg instanceof TextMessage ? ((TextMessage) msg).getText() : msg.getJMSMessageID())); if (batchSize > 0 && received > 0 && received % batchSize == 0) { LOG.info(threadName + " Committing transaction: " + transactions++); session.commit();
Message msg = session.get().createTextMessage(hCatEventMessage.toString()); msg.setStringProperty(HCatConstants.HCAT_MESSAGE_FORMAT, messageFactory.getMessageFormat()); MessageProducer producer = createProducer(topic); producer.send(msg); session.get().commit(); } catch (Exception e) { if (retries >= 0) {
@Test public void testConverter() throws Exception { JmsTemplate template = createTemplate(); template.setConnectionFactory(this.connectionFactory); template.setMessageConverter(new SimpleMessageConverter()); String s = "Hello world"; MessageProducer messageProducer = mock(MessageProducer.class); TextMessage textMessage = mock(TextMessage.class); given(this.session.createProducer(this.queue)).willReturn(messageProducer); given(this.session.createTextMessage("Hello world")).willReturn(textMessage); template.convertAndSend(this.queue, s); verify(messageProducer).send(textMessage); verify(messageProducer).close(); if (useTransactedTemplate()) { verify(this.session).commit(); } verify(this.session).close(); verify(this.connection).close(); }
@Override public void run() { try { consumerConnection.start(); Session consumerSession = consumerConnection.createSession(true, Session.SESSION_TRANSACTED); javax.jms.Queue q1 = consumerSession.createQueue("q1"); MessageConsumer consumer = consumerSession.createConsumer(q1); for (int i = 1; i <= MESSAGE_COUNT; i++) { Message message = consumer.receive(5000); if (message == null) { throw new IOException("No message read in time."); } if (i % 100 == 0) { if (i % 1000 == 0) System.out.println("Read message " + i); consumerSession.commit(); } } // Assure that all messages are consumed consumerSession.commit(); } catch (Exception e) { e.printStackTrace(); errors.incrementAndGet(); } } };
given(this.session.createConsumer(this.queue, messageSelector ? selectorString : null)).willReturn(messageConsumer); given(messageConsumer.receive()).willReturn(textMessage); given(messageConsumer.receive(timeout)).willReturn(textMessage); verify(this.connection).start(); verify(this.connection).close(); if (useTransactedTemplate()) { verify(this.session).commit();
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 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close(); con1.start(); Connection con2 = scf.createConnection(); Session session2 = con2.createSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.commit(); session2.close(); con2.start(); con1.close(); con2.close(); verify(txSession).commit(); verify(txSession).close(); verify(nonTxSession).close(); verify(con).start(); verify(con).stop(); verify(con).close();
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session); tm.commit(ts); verify(session).commit(); verify(session).close(); verify(con).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(); }
@Override public void run() { Session session = null; try { session = connectionConsumer.createSession(true, Session.SESSION_TRANSACTED); MessageConsumer consumer = session.createConsumer(ActiveMQDestination.createQueue("queue2")); for (int i = 0; i < numberOfMessages; i++) { BytesMessage message = (BytesMessage) consumer.receive(5000); if (message == null) { return; } session.commit(); totalCount.incrementAndGet(); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (session != null) { try { session.close(); } catch (Exception e) { e.printStackTrace(); } } } } }.start();
@org.junit.Ignore public void testSendObjectToQueue() throws Exception { final TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class); runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER); runner.setProperty(JmsProperties.URL, "tcp://localhost:61616"); runner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE); runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing"); runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO); WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runner.getProcessContext(), true); final Session jmsSession = wrappedProducer.getSession(); final MessageProducer producer = wrappedProducer.getProducer(); // Revision class is used because test just needs any Serializable class in core NiFi final ObjectMessage message = jmsSession.createObjectMessage(new Revision(1L, "ID", "COMP_ID")); producer.send(message); jmsSession.commit(); producer.close(); jmsSession.close(); } }
final Session jmsSession = wrappedProducer.getSession(); final MessageProducer producer = wrappedProducer.getProducer(); final Message message = jmsSession.createTextMessage("Hello World"); producer.send(message); jmsSession.commit();
Session tas = tac.createSession(false, Session.AUTO_ACKNOWLEDGE); tas.getTransacted(); tas.close(); verify(this.connection).start(); if (useTransactedTemplate()) { verify(this.session).commit();
@Test public void testTransactionCommit() throws JMSException { ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); final Session session = mock(Session.class); given(cf.createConnection()).willReturn(con); given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session); JmsTransactionManager tm = new JmsTransactionManager(cf); TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); JmsTemplate jt = new JmsTemplate(cf); jt.execute(new SessionCallback<Void>() { @Override public Void doInJms(Session sess) { assertTrue(sess == session); return null; } }); tm.commit(ts); verify(session).commit(); verify(session).close(); verify(con).close(); }
@Test public void testRedispatchOfRolledbackTx() throws Exception { connection.start(); Session session = connection.createSession(true, Session.SESSION_TRANSACTED); ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE); sendMessages(connection, destination, 2); MessageConsumer consumer = session.createConsumer(destination); assertNotNull(consumer.receive(1000)); assertNotNull(consumer.receive(1000)); // install another consumer while message dispatch is unacked/uncommitted Session redispatchSession = connection.createSession(true, Session.SESSION_TRANSACTED); MessageConsumer redispatchConsumer = redispatchSession.createConsumer(destination); session.rollback(); session.close(); Message msg = redispatchConsumer.receive(1000); assertNotNull(msg); assertTrue(msg.getJMSRedelivered()); assertEquals(2, msg.getLongProperty("JMSXDeliveryCount")); msg = redispatchConsumer.receive(1000); assertNotNull(msg); assertTrue(msg.getJMSRedelivered()); assertEquals(2, msg.getLongProperty("JMSXDeliveryCount")); redispatchSession.commit(); assertNull(redispatchConsumer.receive(500)); redispatchSession.close(); }
TextMessage textMessage = mock(TextMessage.class); given(this.session.createProducer(this.queue)).willReturn(messageProducer); given(this.session.createTextMessage("just testing")).willReturn(textMessage); verify(this.session).commit(); verify(messageProducer).send(textMessage); verify(messageProducer).send(textMessage, this.qosSettings.getDeliveryMode(), this.qosSettings.getPriority(), this.qosSettings.getTimeToLive());
@org.junit.Ignore public void testSendMapToQueue() throws Exception { final TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class); runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER); runner.setProperty(JmsProperties.URL, "tcp://localhost:61616"); runner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE); runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing"); runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO); WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runner.getProcessContext(), true); final Session jmsSession = wrappedProducer.getSession(); final MessageProducer producer = wrappedProducer.getProducer(); final MapMessage message = jmsSession.createMapMessage(); message.setString("foo!", "bar"); message.setString("bacon", "meat"); producer.send(message); jmsSession.commit(); producer.close(); jmsSession.close(); }
final Session jmsSession = wrappedProducer.getSession(); final MessageProducer producer = wrappedProducer.getProducer(); final Message message = jmsSession.createTextMessage("createMessageDirectly"); producer.send(message); jmsSession.commit();
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session, session2); tm.commit(ts); verify(session).commit(); verify(session2).commit(); verify(session).close(); verify(session2).close();
private void sendToDestination(String message, Destination destination) { Connection connection = null; try { connection = connectionFactory.createConnection(); final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); final javax.jms.MessageProducer producer = TraceeMessageWriter.wrap(session.createProducer(destination)); final TextMessage textMessage = session.createTextMessage(); textMessage.setText(message); LOG.info("I am about to send the message \"{]\" to {}", message, destination.toString()); producer.send(textMessage); session.commit(); } catch (JMSException jmse) { throw new RuntimeException("This example is so cheap, this must not have happened!", jmse); } }