protected void prepare() { if (this.options.jmsProvider == null || this.options.msgProducer == null) { throw new IllegalStateException("JMS Provider and MessageProducer not set."); } LOG.debug("Connecting JMS.."); try { ConnectionFactory cf = this.options.jmsProvider.connectionFactory(); Destination dest = this.options.jmsProvider.destination(); this.connection = cf.createConnection(); this.session = connection.createSession(this.options.jmsTransactional, this.options.jmsAcknowledgeMode); this.messageProducer = session.createProducer(dest); connection.start(); } catch (Exception e) { LOG.warn("Error creating JMS connection.", e); } }
/** * Create the JMS connection * @return newly created JMS connection */ protected Connection createConnection() { LOG.info("Will create new JMS connection"); Context jndiCntxt; Connection jmsConnection = null; try { jndiCntxt = new InitialContext(); ConnectionFactory connFac = (ConnectionFactory) jndiCntxt.lookup("ConnectionFactory"); jmsConnection = connFac.createConnection(); jmsConnection.start(); jmsConnection.setExceptionListener(new ExceptionListener() { @Override public void onException(JMSException jmse) { LOG.error("JMS Exception listener received exception. Ignored the error", jmse); } }); } catch (NamingException e) { LOG.error("JNDI error while setting up Message Bus connection. " + "Please make sure file named 'jndi.properties' is in " + "classpath and contains appropriate key-value pairs.", e); } catch (JMSException e) { LOG.error("Failed to initialize connection to message bus", e); } catch (Throwable t) { LOG.error("Unable to connect to JMS provider", t); } return jmsConnection; }
ConnectionFactory cf = jmsProvider.connectionFactory(); Destination dest = jmsProvider.destination(); connection = cf.createConnection(); session = messageHandler.createSession(connection); consumer = session.createConsumer(dest); connection.start(); } catch (Exception e) { LOG.warn("Error creating JMS connection.", e);
private void unsubscribe(final String url, final String username, final String password, final String subscriptionId, final String jmsProvider, final int timeoutMillis) throws JMSException { final Connection connection; if (username == null && password == null) { connection = JmsFactory.createConnectionFactory(url, timeoutMillis, jmsProvider).createConnection(); } else { connection = JmsFactory.createConnectionFactory(url, timeoutMillis, jmsProvider).createConnection(username, password); } Session session = null; try { connection.setClientID(subscriptionId); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); session.unsubscribe(subscriptionId); getLogger().info("Successfully unsubscribed from {}, Subscription Identifier {}", new Object[]{url, subscriptionId}); } finally { if (session != null) { try { session.close(); } catch (final Exception e1) { getLogger().warn("Unable to close session with JMS Server due to {}; resources may not be cleaned up appropriately", new Object[]{e1}); } } try { connection.close(); } catch (final Exception e1) { getLogger().warn("Unable to close connection to JMS Server due to {}; resources may not be cleaned up appropriately", new Object[]{e1}); } } }
@Test public void testWithConnectionFactory() throws JMSException { ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); given(cf.createConnection()).willReturn(con); SingleConnectionFactory scf = new SingleConnectionFactory(cf); Connection con1 = scf.createConnection(); Connection con2 = scf.createConnection(); con1.start(); con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(con).start(); verify(con).stop(); verify(con).close(); verifyNoMoreInteractions(con); }
public JmsAppenderTest() throws Exception { // this needs to set up before LoggerContextRule given(connectionFactory.createConnection()).willReturn(connection); given(connectionFactory.createConnection(anyString(), anyString())).willThrow(IllegalArgumentException.class); given(connection.createSession(eq(false), eq(Session.AUTO_ACKNOWLEDGE))).willReturn(session); given(session.createProducer(eq(destination))).willReturn(messageProducer); given(session.createTextMessage(anyString())).willReturn(textMessage); given(session.createObjectMessage(isA(Serializable.class))).willReturn(objectMessage); }
@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); }
/** * Create the mock objects for testing. */ @Before public void setupMocks() throws Exception { this.jndiContext = mock(Context.class); this.connectionFactory = mock(ConnectionFactory.class); this.connection = mock(Connection.class); this.session = mock(Session.class); this.queue = mock(Queue.class); given(this.connectionFactory.createConnection()).willReturn(this.connection); given(this.connection.createSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE)).willReturn(this.session); given(this.session.getTransacted()).willReturn(useTransactedSession()); given(this.jndiContext.lookup("testDestination")).willReturn(this.queue); }
ConnectionFactory cf = mock(ConnectionFactory.class); TestConnection con = new TestConnection(); given(cf.createConnection()).willReturn(con); assertSame(listener2, con2.getExceptionListener()); con.getExceptionListener().onException(new JMSException("")); con2.close(); con.getExceptionListener().onException(new JMSException("")); con1.close(); con.getExceptionListener().onException(new JMSException(""));
/** * Initializes JMS resources. */ @Override public void prepare(Map<String, Object> topoConf, TopologyContext context, OutputCollector collector) { if (this.jmsProvider == null || this.producer == null) { throw new IllegalStateException("JMS Provider and MessageProducer not set."); } this.collector = collector; LOG.debug("Connecting JMS.."); try { ConnectionFactory cf = this.jmsProvider.connectionFactory(); Destination dest = this.jmsProvider.destination(); this.connection = cf.createConnection(); this.session = connection.createSession(this.jmsTransactional, this.jmsAcknowledgeMode); this.messageProducer = session.createProducer(dest); connection.start(); } catch (Exception e) { LOG.warn("Error creating JMS connection.", e); } } }
final Session session = mock(Session.class); given(cf.createConnection()).willReturn(con); given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session); verify(con).close();
@Test public void testWithConnectionFactoryAndClientId() throws JMSException { ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); given(cf.createConnection()).willReturn(con); SingleConnectionFactory scf = new SingleConnectionFactory(cf); scf.setClientId("myId"); Connection con1 = scf.createConnection(); Connection con2 = scf.createConnection(); con1.start(); con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(con).setClientID("myId"); verify(con).start(); verify(con).stop(); verify(con).close(); verifyNoMoreInteractions(con); }
when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createSession(true, Session.AUTO_ACKNOWLEDGE)).thenReturn(session); when(session.createProducer(any())).thenReturn(producer); when(session.createTextMessage(any())).thenReturn(message);
public static Connection createConnection(final ProcessContext context, final String clientId) throws JMSException { Objects.requireNonNull(context); Objects.requireNonNull(clientId); final ConnectionFactory connectionFactory = createConnectionFactory(context); final String username = context.getProperty(USERNAME).getValue(); final String password = context.getProperty(PASSWORD).getValue(); final Connection connection = (username == null && password == null) ? connectionFactory.createConnection() : connectionFactory.createConnection(username, password); connection.setClientID(clientId); connection.start(); return connection; }
}).when(connectionFactory).createConnection(); Session session = mock(Session.class); when(connection.createSession(false, 1)).thenReturn(session); MessageConsumer consumer = mock(MessageConsumer.class); when(session.createConsumer(any(Destination.class), isNull())).thenReturn(consumer); when(session.createTemporaryQueue()).thenReturn(mock(TemporaryQueue.class)); final Message message = mock(Message.class);
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();
final Session session = mock(Session.class); given(cf.createConnection()).willReturn(con); given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session); verify(con).close();
@Test public void testWithConnectionFactoryAndExceptionListener() throws JMSException { ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); ExceptionListener listener = new ChainedExceptionListener(); given(cf.createConnection()).willReturn(con); given(con.getExceptionListener()).willReturn(listener); SingleConnectionFactory scf = new SingleConnectionFactory(cf); scf.setExceptionListener(listener); Connection con1 = scf.createConnection(); assertEquals(listener, con1.getExceptionListener()); con1.start(); con1.stop(); con1.close(); Connection con2 = scf.createConnection(); con2.start(); con2.stop(); con2.close(); scf.destroy(); // should trigger actual close verify(con).setExceptionListener(listener); verify(con, times(2)).start(); verify(con, times(2)).stop(); verify(con).close(); }
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);
connection = connectionFactory.createConnection(); if (clientId != null && clientId.trim().length() > 0) { connection.setClientID(clientId.trim()); connection.start();