public void stop() throws JMSException { connection.close(); try { broker.stop(); } catch (Exception e) { } } }
synchronized public void close() { if( connection!=null ) { Iterator<SessionPool> i = cache.values().iterator(); while (i.hasNext()) { SessionPool pool = i.next(); i.remove(); try { pool.close(); } catch (Exception e) { } } try { connection.close(); } catch (Exception e) { } connection = null; } }
@Override public synchronized void stop() throws ConnectorException { if (conn != null) try { conn.close(); } catch (JMSException e) { throw new ConnectorException(e.getMessage()); } }
public void clearQueue(final String queueName) { ActiveMQConnection connection = null; try { connection = (ActiveMQConnection) connectionFactory.createConnection(); connection.destroyDestination(new ActiveMQQueue(queueName)); } catch (JMSException ex) { logger.error("clearing all messages on queue " + queueName, ex); } finally { if (connection != null) { try { connection.close(); } catch (JMSException ex) { logger.error("Closing JMS connection", ex); } } } }
@Override public void onMessage(Message m) { try { TextMessage tm = (TextMessage) m; assertEquals("" + counter.get(), tm.getText()); counter.incrementAndGet(); if (counter.get() == 2) { sendDone.await(); connection.close(); got2Done.countDown(); } System.out.println("acking tm: " + tm.getText()); tm.acknowledge(); } catch (Throwable e) { System.out.println("ack failed!!"); e.printStackTrace(); } } });
@Override public void onMessage(Message m) { try { TextMessage tm = (TextMessage) m; assertEquals("" + counter.get(), tm.getText()); counter.incrementAndGet(); m.acknowledge(); if (counter.get() == 2) { sendDone.await(); connection.close(); got2Done.countDown(); } } catch (Throwable e) { e.printStackTrace(); } } });
try { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616/"); ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection(); DestinationSource ds = connection.getDestinationSource(); connection.start(); Set<ActiveMQQueue> queues = ds.getQueues(); for (ActiveMQQueue activeMQQueue : queues) { try { System.out.println(activeMQQueue.getQueueName()); } catch (JMSException e) { e.printStackTrace(); } } connection.close(); } catch (Exception e) { e.printStackTrace(); }
@Override @After public void tearDown() throws Exception { if (sameIdConnection != null) { sameIdConnection.close(); sameIdConnection = null; } super.tearDown(); }
private BrokerInfo getBrokerInfo(PortletRequest portletRequest, JMSDestinationInfo destinationInfo) throws JMSException { ActiveMQConnectionFactory connectionFactory = createActiveMQConnectionFactory(portletRequest, destinationInfo); ActiveMQConnection connection = null; try { connection = (ActiveMQConnection) connectionFactory.createConnection(); connection.start(); return connection.getBrokerInfo(); } finally { if (connection != null) { try { connection.close(); } catch (Exception e) { } } } } }
@Test(timeout = 10000) public void testAutoCreatedQueue() throws JMSException { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("autoCreated"); // Reset the session. session.close(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); session.createConsumer(queue); session.close(); connection.close(); //Happens more than once because of advisories verifier.validatePluginMethodsAtLeast(2, BEFORE_ADD_ADDRESS, AFTER_ADD_ADDRESS, BEFORE_REMOVE_ADDRESS, AFTER_REMOVE_ADDRESS); }
protected void reconnect() throws Exception { if (connection != null) { // Close the prev connection. connection.close(); } session = null; connection = (ActiveMQConnection) resourceProvider.createConnection(this.factory); reconnectSession(); connection.start(); }
@Test(timeout = 10000) public void testAckedMessageAreConsumed() throws JMSException { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(queueName); MessageProducer producer = session.createProducer(queue); producer.send(session.createTextMessage("Hello")); // Consume the message... MessageConsumer consumer = session.createConsumer(queue); Message msg = consumer.receive(1000); assertNotNull(msg); // Reset the session. session.close(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Attempt to Consume the message... consumer = session.createConsumer(queue); msg = consumer.receive(1000); assertNull(msg); session.close(); connection.close(); verifier.validatePluginMethodsEquals(0, MESSAGE_EXPIRED, BEFORE_DEPLOY_BRIDGE, AFTER_DEPLOY_BRIDGE, BEFORE_SESSION_METADATA_ADDED, AFTER_SESSION_METADATA_ADDED); verifier.validatePluginMethodsAtLeast(1, AFTER_CREATE_CONNECTION, AFTER_DESTROY_CONNECTION, BEFORE_CREATE_SESSION, AFTER_CREATE_SESSION, BEFORE_CLOSE_SESSION, AFTER_CLOSE_SESSION, BEFORE_CREATE_CONSUMER, AFTER_CREATE_CONSUMER, BEFORE_CLOSE_CONSUMER, AFTER_CLOSE_CONSUMER, BEFORE_CREATE_QUEUE, AFTER_CREATE_QUEUE, MESSAGE_ACKED, BEFORE_SEND, AFTER_SEND, BEFORE_MESSAGE_ROUTE, AFTER_MESSAGE_ROUTE, BEFORE_DELIVER, AFTER_DELIVER, BEFORE_ADD_ADDRESS, AFTER_ADD_ADDRESS, BEFORE_ADD_BINDING, AFTER_ADD_BINDING, BEFORE_REMOVE_BINDING, AFTER_REMOVE_BINDING); }
@Test public void testReconnectMultipleTimesWithSameClientID() throws Exception { try { sameIdConnection = (ActiveMQConnection) this.factory.createConnection(); useConnection(sameIdConnection); // now lets create another which should fail for (int i = 1; i < 11; i++) { Connection connection2 = this.factory.createConnection(); try { useConnection(connection2); fail("Should have thrown InvalidClientIDException on attempt" + i); } catch (InvalidClientIDException e) { System.err.println("Caught expected: " + e); } finally { connection2.close(); } } // now lets try closing the original connection and creating a new // connection with the same ID sameIdConnection.close(); sameIdConnection = (ActiveMQConnection) factory.createConnection(); useConnection(connection); } finally { if (sameIdConnection != null) { sameIdConnection.close(); } } }
@Override @After public void tearDown() throws Exception { try { if (flowControlConnection != null) { TcpTransport t = flowControlConnection.getTransport().narrow(TcpTransport.class); try { flowControlConnection.getTransport().stop(); flowControlConnection.close(); } catch (Throwable ignored) { // sometimes the disposed up can make the test to fail // even worse I have seen this breaking every single test after this // if not caught here } t.getTransportListener().onException(new IOException("Disposed.")); } if (asyncThread != null) { asyncThread.join(); asyncThread = null; } } finally { super.tearDown(); } }
@Override @After public void tearDown() throws Exception { try { if (flowControlConnection != null) { TcpTransport t = flowControlConnection.getTransport().narrow(TcpTransport.class); try { flowControlConnection.getTransport().stop(); flowControlConnection.close(); } catch (Throwable ignored) { } } } finally { super.tearDown(); } }
@Test public void testTempTopicDelete() throws Exception { connection.start(); TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TemporaryTopic tempTopic = topicSession.createTemporaryTopic(); ActiveMQConnection newConn = (ActiveMQConnection) factory.createConnection(); try { TopicSession newTopicSession = newConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher publisher = newTopicSession.createPublisher(tempTopic); // need to wait here because the ActiveMQ client's temp destination map is updated asynchronously, not waiting can introduce a race assertTrue(Wait.waitFor(() -> newConn.activeTempDestinations.size() == 1, 2000, 100)); TextMessage msg = newTopicSession.createTextMessage("Test Message"); publisher.publish(msg); try { TopicSubscriber consumer = newTopicSession.createSubscriber(tempTopic); fail("should have gotten exception but got consumer: " + consumer); } catch (JMSException ex) { //correct } connection.close(); try { Message newMsg = newTopicSession.createMessage(); publisher.publish(newMsg); } catch (JMSException e) { //ok } } finally { newConn.close(); } }
@Test public void testAutoDestinationCreationAndDeletionOnConsumer() throws Exception { AddressSettings addressSetting = new AddressSettings(); addressSetting.setAutoCreateQueues(true); addressSetting.setAutoCreateAddresses(true); addressSetting.setAutoDeleteQueues(true); addressSetting.setAutoDeleteAddresses(true); String address = "foo"; server.getAddressSettingsRepository().addMatch(address, addressSetting); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); TextMessage message = session.createTextMessage("bar"); Queue queue = new ActiveMQQueue(address); MessageConsumer consumer = session.createConsumer(queue); assertTrue(Wait.waitFor(() -> (server.locateQueue(SimpleString.toSimpleString("foo")) != null), 2000, 100)); assertTrue(Wait.waitFor(() -> (server.getAddressInfo(SimpleString.toSimpleString("foo")) != null), 2000, 100)); MessageProducer producer = session.createProducer(null); producer.send(queue, message); TextMessage message1 = (TextMessage) consumer.receive(1000); assertTrue(message1.getText().equals(message.getText())); assertNotNull(server.locateQueue(SimpleString.toSimpleString("foo"))); consumer.close(); connection.close(); assertTrue(Wait.waitFor(() -> (server.locateQueue(SimpleString.toSimpleString("foo")) == null), 2000, 100)); assertTrue(Wait.waitFor(() -> (server.getAddressInfo(SimpleString.toSimpleString("foo")) == null), 2000, 100)); }