Java Code Examples for org.apache.activemq.ActiveMQConnection#close()
The following examples show how to use
org.apache.activemq.ActiveMQConnection#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FailoverTimeoutTest.java From activemq-artemis with Apache License 2.0 | 6 votes |
@Test public void testUpdateUris() throws Exception { ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + tcpUri + ")?useExponentialBackOff=false"); ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection(); try { connection.start(); FailoverTransport failoverTransport = connection.getTransport().narrow(FailoverTransport.class); URI[] bunchOfUnknownAndOneKnown = new URI[]{new URI("tcp://unknownHost:" + tcpUri.getPort()), new URI("tcp://unknownHost2:" + tcpUri.getPort()), new URI("tcp://localhost:2222")}; failoverTransport.add(false, bunchOfUnknownAndOneKnown); } finally { if (connection != null) { connection.close(); } } }
Example 2
Source File: MessageCompressionTest.java From activemq-artemis with Apache License 2.0 | 6 votes |
private void sendTestMapMessage(ActiveMQConnectionFactory factory, String message) throws JMSException { ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(queue); MapMessage mapMessage = session.createMapMessage(); mapMessage.setBoolean("boolean-type", true); mapMessage.setByte("byte-type", (byte) 10); mapMessage.setBytes("bytes-type", TEXT.getBytes()); mapMessage.setChar("char-type", 'A'); mapMessage.setDouble("double-type", 55.3D); mapMessage.setFloat("float-type", 79.1F); mapMessage.setInt("int-type", 37); mapMessage.setLong("long-type", 56652L); mapMessage.setObject("object-type", new String("VVVV")); mapMessage.setShort("short-type", (short) 333); mapMessage.setString("string-type", TEXT); producer.send(mapMessage); connection.close(); }
Example 3
Source File: MessageCompressionTest.java From activemq-artemis with Apache License 2.0 | 6 votes |
private void sendTestStreamMessage(ActiveMQConnectionFactory factory, String message) throws JMSException { ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(queue); StreamMessage streamMessage = session.createStreamMessage(); streamMessage.writeBoolean(true); streamMessage.writeByte((byte) 10); streamMessage.writeBytes(TEXT.getBytes()); streamMessage.writeChar('A'); streamMessage.writeDouble(55.3D); streamMessage.writeFloat(79.1F); streamMessage.writeInt(37); streamMessage.writeLong(56652L); streamMessage.writeObject(new String("VVVV")); streamMessage.writeShort((short) 333); streamMessage.writeString(TEXT); producer.send(streamMessage); connection.close(); }
Example 4
Source File: TwoBrokerFailoverClusterTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
@After public void tearDown() throws Exception { for (ActiveMQConnection conn : connections) { conn.close(); } server0.stop(); server1.stop(); }
Example 5
Source File: SimpleOpenWireTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
@Test public void testTempQueueDelete() throws Exception { connection.start(); QueueSession queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); TemporaryQueue tempQueue = queueSession.createTemporaryQueue(); ActiveMQConnection newConn = (ActiveMQConnection) factory.createConnection(); try { QueueSession newQueueSession = newConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); QueueSender queueSender = newQueueSession.createSender(tempQueue); Message msg = queueSession.createMessage(); queueSender.send(msg); try { QueueReceiver consumer = newQueueSession.createReceiver(tempQueue); fail("should have gotten exception but got consumer: " + consumer); } catch (JMSException ex) { //correct } connection.close(); try { Message newMsg = newQueueSession.createMessage(); queueSender.send(newMsg); } catch (JMSException e) { //ok } } finally { newConn.close(); } }
Example 6
Source File: MessageCompressionTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
private ActiveMQStreamMessage receiveTestStreamMessage(ActiveMQConnectionFactory factory) throws JMSException { ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queue); ActiveMQStreamMessage rc = (ActiveMQStreamMessage) consumer.receive(); connection.close(); return rc; }
Example 7
Source File: MessageCompressionTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
private ActiveMQTextMessage receiveTestMessage(ActiveMQConnectionFactory factory) throws JMSException { ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queue); ActiveMQTextMessage rc = (ActiveMQTextMessage) consumer.receive(); connection.close(); return rc; }
Example 8
Source File: MessageCompressionTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
private void sendTestObjectMessage(ActiveMQConnectionFactory factory, String message) throws JMSException { ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(queue); ObjectMessage objectMessage = session.createObjectMessage(); objectMessage.setObject(TEXT); producer.send(objectMessage); connection.close(); }
Example 9
Source File: TestBroker.java From pinpoint with Apache License 2.0 | 5 votes |
void stop() throws Exception { for (Map.Entry<String, ActiveMQConnection> e : this.connections.entrySet()) { ActiveMQConnection connection = e.getValue(); connection.close(); } this.brokerService.stop(); this.brokerService.waitUntilStopped(); }
Example 10
Source File: MessageCompressionTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
private void sendTestBytesMessage(ActiveMQConnectionFactory factory, String message) throws JMSException, UnsupportedEncodingException { ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(queue); BytesMessage bytesMessage = session.createBytesMessage(); bytesMessage.writeBytes(message.getBytes(StandardCharsets.UTF_8)); producer.send(bytesMessage); connection.close(); }
Example 11
Source File: FailoverConsumerOutstandingCommitTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
private Message receiveMessage(ActiveMQConnectionFactory cf, Queue destination) throws Exception { final ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection(); connection.start(); final Session consumerSession = connection.createSession(true, Session.SESSION_TRANSACTED); final MessageConsumer consumer = consumerSession.createConsumer(destination); Message msg = consumer.receive(5000); consumerSession.commit(); connection.close(); return msg; }
Example 12
Source File: MessageCompressionTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
private ActiveMQMapMessage receiveTestMapMessage(ActiveMQConnectionFactory factory) throws JMSException { ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queue); ActiveMQMapMessage rc = (ActiveMQMapMessage) consumer.receive(); connection.close(); return rc; }
Example 13
Source File: FailoverPrefetchZeroTest.java From activemq-artemis with Apache License 2.0 | 4 votes |
@Test @BMRules( rules = {@BMRule( name = "set no return response and stop the broker", targetClass = "org.apache.activemq.artemis.core.protocol.openwire.OpenWireConnection$CommandProcessor", targetMethod = "processMessagePull", targetLocation = "ENTRY", action = "org.apache.activemq.transport.failover.FailoverPrefetchZeroTest.holdResponseAndStopBroker($0)")}) public void testPrefetchZeroConsumerThroughRestart() throws Exception { broker = createBroker(); broker.start(); doByteman.set(true); ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")"); cf.setWatchTopicAdvisories(false); final ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection(); connection.start(); final Session consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); final Queue destination = consumerSession.createQueue(QUEUE_NAME + "?consumer.prefetchSize=" + prefetch); final MessageConsumer consumer = consumerSession.createConsumer(destination); produceMessage(consumerSession, destination, 1); final CountDownLatch receiveDone = new CountDownLatch(1); final Vector<Message> received = new Vector<>(); new Thread() { @Override public void run() { try { LOG.info("receive one..."); Message msg = consumer.receive(30000); if (msg != null) { received.add(msg); } receiveDone.countDown(); LOG.info("done receive"); } catch (Exception e) { e.printStackTrace(); } } }.start(); // will be stopped by the plugin assertTrue("pull completed on broker", pullDone.await(30, TimeUnit.SECONDS)); brokerStopLatch.await(); doByteman.set(false); broker = createBroker(); broker.start(); assertTrue("receive completed through failover", receiveDone.await(30, TimeUnit.SECONDS)); assertTrue("we got our message:", !received.isEmpty()); connection.close(); }
Example 14
Source File: MQTTOpenwireTest.java From activemq-artemis with Apache License 2.0 | 4 votes |
public void doTestSendJMSReceiveMQTT(String jmsDestination, String mqttTopic) throws Exception { final MQTTClientProvider provider = getMQTTClientProvider(); initializeConnection(provider); ActiveMQConnection activeMQConnection = (ActiveMQConnection) cf.createConnection(); try { activeMQConnection.setUseRetroactiveConsumer(true); activeMQConnection.start(); Session s = activeMQConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); javax.jms.Topic jmsTopic = s.createTopic(jmsDestination); MessageProducer producer = s.createProducer(jmsTopic); final String RETAINED = "RETAINED"; provider.subscribe(mqttTopic, AT_MOST_ONCE); // send retained message from JMS TextMessage sendMessage = s.createTextMessage(RETAINED); // mark the message to be retained sendMessage.setBooleanProperty("ActiveMQ.Retain", true); // MQTT QoS can be set using MQTTProtocolConverter.QOS_PROPERTY_NAME property sendMessage.setIntProperty("ActiveMQ.MQTT.QoS", 0); producer.send(sendMessage); byte[] message = provider.receive(2000); assertNotNull("Should get retained message " + jmsDestination + "->" + mqttTopic, message); assertEquals(RETAINED, new String(message)); for (int i = 0; i < 1; i++) { String payload = "This is Test Message: " + i; sendMessage = s.createTextMessage(payload); producer.send(sendMessage); message = provider.receive(1000); assertNotNull("Should get a message " + jmsDestination + "->" + mqttTopic, message); assertEquals(payload, new String(message)); } } finally { activeMQConnection.close(); provider.disconnect(); } }
Example 15
Source File: TwoBrokerTempQueueAdvisoryTest.java From activemq-artemis with Apache License 2.0 | 4 votes |
public void testSendToRemovedTemp() throws Exception { ActiveMQQueue requestReplyDest = new ActiveMQQueue("RequestReply"); NetworkConnector nc = bridgeBrokers("BrokerA", "BrokerB"); if (useDuplex) { nc.setDuplex(true); } else { bridgeBrokers("BrokerB", "BrokerA"); } // destination advisory can loose the race with message dispatch, so we need to allow replies on network broker // to work in the absence of an advisory, the destination will be cleaned up in the normal // way if (!useDuplex) { brokers.get("BrokerB").broker.setAllowTempAutoCreationOnSend(true); } TransportConnector forClient = brokers.get("BrokerA").broker.addConnector("tcp://localhost:0"); startAllBrokers(); waitForBridgeFormation(); waitForMinTopicRegionConsumerCount("BrokerB", 1); waitForMinTopicRegionConsumerCount("BrokerA", 1); ConnectionFactory factory = new ActiveMQConnectionFactory(forClient.getConnectUri()); ActiveMQConnection conn = (ActiveMQConnection) factory.createConnection(); conn.setWatchTopicAdvisories(false); conn.start(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); ConnectionFactory replyFactory = getConnectionFactory("BrokerB"); for (int i = 0; i < 500; i++) { TemporaryQueue tempDest = session.createTemporaryQueue(); MessageProducer producer = session.createProducer(requestReplyDest); javax.jms.Message message = session.createTextMessage("req-" + i); message.setJMSReplyTo(tempDest); ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(tempDest); producer.send(message); ActiveMQConnection replyConnection = (ActiveMQConnection) replyFactory.createConnection(); replyConnection.setWatchTopicAdvisories(false); replyConnection.start(); Session replySession = replyConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQMessageConsumer replyConsumer = (ActiveMQMessageConsumer) replySession.createConsumer(requestReplyDest); javax.jms.Message msg = replyConsumer.receive(10000); assertNotNull("request message not null: " + i, msg); MessageProducer replyProducer = replySession.createProducer(msg.getJMSReplyTo()); replyProducer.send(session.createTextMessage("reply-" + i)); replyConnection.close(); javax.jms.Message reply = consumer.receive(10000); assertNotNull("reply message : " + i + ", to: " + tempDest + ", by consumer:" + consumer.getConsumerId(), reply); consumer.close(); tempDest.delete(); } }
Example 16
Source File: RequestReplyNoAdvisoryNetworkTest.java From activemq-artemis with Apache License 2.0 | 4 votes |
public void doTestNonAdvisoryNetworkRequestReply() throws Exception { waitForBridgeFormation(a, 1, 0); waitForBridgeFormation(b, 1, 0); ActiveMQConnectionFactory sendFactory = createConnectionFactory(a); ActiveMQConnection sendConnection = createConnection(sendFactory); ActiveMQSession sendSession = (ActiveMQSession) sendConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = sendSession.createProducer(sendQ); ActiveMQTempQueue realReplyQ = (ActiveMQTempQueue) sendSession.createTemporaryQueue(); TextMessage message = sendSession.createTextMessage("1"); message.setJMSReplyTo(realReplyQ); producer.send(message); LOG.info("request sent"); // responder ActiveMQConnectionFactory consumerFactory = createConnectionFactory(b); ActiveMQConnection consumerConnection = createConnection(consumerFactory); ActiveMQSession consumerSession = (ActiveMQSession) consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = consumerSession.createConsumer(sendQ); TextMessage received = (TextMessage) consumer.receive(receiveTimeout); assertNotNull("got request from sender ok", received); LOG.info("got request, sending reply"); MessageProducer consumerProducer = consumerSession.createProducer(received.getJMSReplyTo()); consumerProducer.send(consumerSession.createTextMessage("got " + received.getText())); // temp dest on reply broker tied to this connection, setOptimizedDispatch=true ensures // message gets delivered before destination is removed consumerConnection.close(); // reply consumer MessageConsumer replyConsumer = sendSession.createConsumer(realReplyQ); TextMessage reply = (TextMessage) replyConsumer.receive(receiveTimeout); assertNotNull("expected reply message", reply); assertEquals("text is as expected", "got 1", reply.getText()); sendConnection.close(); LOG.info("checking for dangling temp destinations"); // ensure all temp dests get cleaned up on all brokers for (BrokerService brokerService : brokers) { final RegionBroker regionBroker = (RegionBroker) brokerService.getRegionBroker(); assertTrue("all temps are gone on " + regionBroker.getBrokerName(), Wait.waitFor(new Wait.Condition() { @Override public boolean isSatisified() throws Exception { Map<?, ?> tempTopics = regionBroker.getTempTopicRegion().getDestinationMap(); LOG.info("temp topics on " + regionBroker.getBrokerName() + ", " + tempTopics); Map<?, ?> tempQ = regionBroker.getTempQueueRegion().getDestinationMap(); LOG.info("temp queues on " + regionBroker.getBrokerName() + ", " + tempQ); return tempQ.isEmpty() && tempTopics.isEmpty(); } })); } }
Example 17
Source File: FailoverConsumerOutstandingCommitTest.java From activemq-artemis with Apache License 2.0 | 4 votes |
@Test public void testRollbackFailoverConsumerTx() throws Exception { server = createBroker(); server.start(); ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("failover:(" + url + ")"); cf.setConsumerFailoverRedeliveryWaitPeriod(10000); final ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection(); connection.start(); final Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); final Queue destination = producerSession.createQueue(QUEUE_NAME); final Session consumerSession = connection.createSession(true, Session.SESSION_TRANSACTED); final MessageConsumer testConsumer = consumerSession.createConsumer(destination); assertNull("no message yet", testConsumer.receiveNoWait()); produceMessage(producerSession, destination, 1); producerSession.close(); // consume then rollback after restart Message msg = testConsumer.receive(5000); assertNotNull(msg); // restart with outstanding delivered message server.stop(); server = createBroker(); server.start(); consumerSession.rollback(); // receive again msg = testConsumer.receive(10000); assertNotNull("got message again after rollback", msg); consumerSession.commit(); // close before sweep consumerSession.close(); msg = receiveMessage(cf, destination); assertNull("should be nothing left after commit", msg); connection.close(); }
Example 18
Source File: DurablePersistentFalseRestartTest.java From activemq-artemis with Apache License 2.0 | 4 votes |
public void testValidateNoPersistenceForDurableAfterRestart() throws Exception { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("failover:(" + broker.getTransportConnectors().get(0).getPublishableConnectString() + ")"); ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection(); connection.setClientID("clientId"); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic destination = session.createTopic(queueName); MessageConsumer consumer = session.createDurableSubscriber(destination, "subscriberName"); populateDestination(10, destination, connection); restartBroker(); // make failover aware of the restarted auto assigned port connection.getTransport().narrow(FailoverTransport.class).add(true, broker.getTransportConnectors().get(0).getPublishableConnectString()); TextMessage msg = (TextMessage) consumer.receive(4000); assertNull("did not get a message when persistent=false, message: " + msg, msg); connection.close(); }
Example 19
Source File: JMSConsumer11Test.java From activemq-artemis with Apache License 2.0 | 4 votes |
@Test public void testPrefetch1MessageNotDispatched() throws Exception { // Set prefetch to 1 connection.getPrefetchPolicy().setAll(1); connection.start(); Session session = connection.createSession(true, 0); ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE); ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer)session.createConsumer(destination); // Send 2 messages to the destination. sendMessages(session, destination, 2); session.commit(); // The prefetch should fill up with 1 message. // Since prefetch is still full, the 2nd message should get dispatched // to another consumer.. lets create the 2nd consumer test that it does // make sure it does. ActiveMQConnection connection2 = (ActiveMQConnection) factory.createConnection(); connection2.start(); Session session2 = connection2.createSession(true, 0); ActiveMQMessageConsumer consumer2 = (ActiveMQMessageConsumer)session2.createConsumer(destination); // On a test race you could have a scenario where the message only arrived at the first consumer and // if the test is not fast enough the first consumer will receive the message againt // This will guarantee the test is correctly balanced. Wait.assertEquals(1, consumer::getMessageSize); Wait.assertEquals(1, consumer2::getMessageSize); // Pick up the first message. Message message1 = consumer.receive(1000); assertNotNull(message1); // Pick up the 2nd messages. Message message2 = consumer2.receive(5000); assertNotNull(message2); session.commit(); session2.commit(); Message m = consumer.receiveNoWait(); assertNull(m); try { connection2.close(); } catch (Throwable e) { System.err.println("exception e: " + e); e.printStackTrace(); } }
Example 20
Source File: BrokerRedeliveryTest.java From activemq-artemis with Apache License 2.0 | 4 votes |
public void doTestScheduledRedelivery(int maxBrokerRedeliveriesToValidate, boolean validateDLQ) throws Exception { startBroker(true); sendMessage(0); ActiveMQConnection consumerConnection = (ActiveMQConnection) createConnection(); RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy(); redeliveryPolicy.setInitialRedeliveryDelay(0); redeliveryPolicy.setMaximumRedeliveries(0); consumerConnection.setRedeliveryPolicy(redeliveryPolicy); consumerConnection.start(); Session consumerSession = consumerConnection.createSession(true, Session.SESSION_TRANSACTED); MessageConsumer consumer = consumerSession.createConsumer(destination); Message message = consumer.receive(1000); assertNotNull("got message", message); LOG.info("got: " + message); consumerSession.rollback(); for (int i = 0; i < maxBrokerRedeliveriesToValidate; i++) { Message shouldBeNull = consumer.receive(500); assertNull("did not get message after redelivery count exceeded: " + shouldBeNull, shouldBeNull); TimeUnit.SECONDS.sleep(3); Message brokerRedeliveryMessage = consumer.receive(500); LOG.info("got: " + brokerRedeliveryMessage); assertNotNull("got message via broker redelivery after delay", brokerRedeliveryMessage); assertEquals("message matches", message.getStringProperty("data"), brokerRedeliveryMessage.getStringProperty("data")); assertEquals("has expiryDelay specified", i == 0 ? initialRedeliveryDelayMillis : redeliveryDelayMillis, brokerRedeliveryMessage.getLongProperty(RedeliveryPlugin.REDELIVERY_DELAY)); consumerSession.rollback(); } if (validateDLQ) { MessageConsumer dlqConsumer = consumerSession.createConsumer(new ActiveMQQueue(SharedDeadLetterStrategy.DEFAULT_DEAD_LETTER_QUEUE_NAME)); Message dlqMessage = dlqConsumer.receive(2000); assertNotNull("Got message from dql", dlqMessage); assertEquals("message matches", message.getStringProperty("data"), dlqMessage.getStringProperty("data")); consumerSession.commit(); } else { // consume/commit ok message = consumer.receive(3000); assertNotNull("got message", message); assertEquals("redeliveries accounted for", maxBrokerRedeliveriesToValidate + 2, message.getLongProperty("JMSXDeliveryCount")); consumerSession.commit(); } consumerConnection.close(); }