Here are the examples of the java api class org.apache.activemq.ActiveMQSession taken from open source projects.
1. ActiveMQClientITBase#testQueuePull()
Project: pinpoint
File: ActiveMQClientITBase.java
File: ActiveMQClientITBase.java
@Test public void testQueuePull() throws Exception { // Given final String testQueueName = "TestPullQueue"; final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName); final String testMessage = "Hello World for Queue!"; // create producer ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl()); MessageProducer producer = producerSession.createProducer(testQueue); final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage); // When ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl()); MessageConsumer consumer = consumerSession.createConsumer(testQueue); // Then producer.send(expectedTextMessage); Message message = consumer.receive(1000L); Assert.assertEquals(testMessage, ((TextMessage) message).getText()); // Wait till all traces are recorded (consumer traces are recorded from another thread) awaitAndVerifyTraceCount(5, 5000L); // trace count : 1 verifyProducerSendEvent(testQueue); // trace count : 4 verifyConsumerPullEvent(testQueue, consumer, expectedTextMessage); }
2. ActiveMQClientITBase#testTopicPush()
Project: pinpoint
File: ActiveMQClientITBase.java
File: ActiveMQClientITBase.java
@Test public void testTopicPush() throws Exception { // Given final String testTopicName = "TestPushTopic"; final ActiveMQTopic testTopic = new ActiveMQTopic(testTopicName); final String testMessage = "Hello World for Topic!"; final int numMessageConsumers = 2; final CountDownLatch consumerConsumeLatch = new CountDownLatch(numMessageConsumers); final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>(); // create producer ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl()); MessageProducer producer = new MessageProducerBuilder(producerSession, testTopic).waitTillStarted().build(); final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage); // create 2 consumers ActiveMQSession consumer1Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl()); new MessageConsumerBuilder(consumer1Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build(); ActiveMQSession consumer2Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl()); new MessageConsumerBuilder(consumer2Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build(); // When producer.send(expectedTextMessage); consumerConsumeLatch.await(1L, TimeUnit.SECONDS); // Then // Wait till all traces are recorded (consumer traces are recorded from another thread) awaitAndVerifyTraceCount(3, 1000L); // trace count : 1 verifyProducerSendEvent(testTopic); // trace count : 1 verifyConsumerPushEvent(testTopic); // trace count : 1 verifyConsumerPushEvent(testTopic); }
3. ActiveMQClientITBase#testTopicPull()
Project: pinpoint
File: ActiveMQClientITBase.java
File: ActiveMQClientITBase.java
@Test public void testTopicPull() throws Exception { // Given final String testTopicName = "TestPullTopic"; final ActiveMQTopic testTopic = new ActiveMQTopic(testTopicName); final String testMessage = "Hello World for Topic!"; // create producer ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl()); MessageProducer producer = new MessageProducerBuilder(producerSession, testTopic).waitTillStarted().build(); final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage); // create 2 consumers ActiveMQSession consumer1Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl()); MessageConsumer consumer1 = new MessageConsumerBuilder(consumer1Session, testTopic).waitTillStarted().build(); ActiveMQSession consumer2Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl()); MessageConsumer consumer2 = new MessageConsumerBuilder(consumer2Session, testTopic).waitTillStarted().build(); // When producer.send(expectedTextMessage); Message message1 = consumer1.receive(1000L); Message message2 = consumer2.receive(1000L); Assert.assertEquals(testMessage, ((TextMessage) message1).getText()); Assert.assertEquals(testMessage, ((TextMessage) message2).getText()); // Wait till all traces are recorded (consumer traces are recorded from another thread) awaitAndVerifyTraceCount(9, 5000L); // trace count : 1 verifyProducerSendEvent(testTopic); // trace count : 4 verifyConsumerPullEvent(testTopic, consumer1, expectedTextMessage); // trace count : 4 verifyConsumerPullEvent(testTopic, consumer2, expectedTextMessage); }
4. ActiveMQClientITBase#testQueuePush()
Project: pinpoint
File: ActiveMQClientITBase.java
File: ActiveMQClientITBase.java
@Test public void testQueuePush() throws Exception { // Given final String testQueueName = "TestPushQueue"; final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName); final String testMessage = "Hello World for Queue!"; final CountDownLatch consumerLatch = new CountDownLatch(1); final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>(); // create producer ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl()); MessageProducer producer = producerSession.createProducer(testQueue); final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage); // create consumer ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl()); MessageConsumer consumer = consumerSession.createConsumer(testQueue); consumer.setMessageListener(new AssertTextMessageListener(consumerLatch, consumerThrowables, expectedTextMessage)); // When producer.send(expectedTextMessage); consumerLatch.await(1L, TimeUnit.SECONDS); // Then assertNoConsumerError(consumerThrowables); // Wait till all traces are recorded (consumer traces are recorded from another thread) awaitAndVerifyTraceCount(2, 5000L); // trace count : 1 verifyProducerSendEvent(testQueue); // trace count : 1 verifyConsumerPushEvent(testQueue); }
5. RequestReplyNoAdvisoryNetworkTest#doTestNonAdvisoryNetworkRequestReply()
Project: activemq-artemis
File: RequestReplyNoAdvisoryNetworkTest.java
File: RequestReplyNoAdvisoryNetworkTest.java
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(); } })); } }
6. ActiveMQMessageConsumerDispatchInterceptor#doInBeforeTrace()
Project: pinpoint
File: ActiveMQMessageConsumerDispatchInterceptor.java
File: ActiveMQMessageConsumerDispatchInterceptor.java
@Override protected void doInBeforeTrace(SpanRecorder recorder, Object target, Object[] args) { recorder.recordServiceType(ActiveMQClientConstants.ACTIVEMQ_CLIENT); ActiveMQSession session = ((ActiveMQSessionGetter) target)._$PINPOINT$_getActiveMQSession(); ActiveMQConnection connection = session.getConnection(); Transport transport = getRootTransport(((TransportGetter) connection)._$PINPOINT$_getTransport()); Socket socket = ((SocketGetter) transport)._$PINPOINT$_getSocket(); SocketAddress localSocketAddress = socket.getLocalSocketAddress(); String endPoint = ActiveMQClientUtils.getEndPoint(localSocketAddress); // Endpoint should be the local socket address of the consumer. recorder.recordEndPoint(endPoint); SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress(); String remoteAddress = ActiveMQClientUtils.getEndPoint(remoteSocketAddress); // Remote address is the socket address of where the consumer is connected to. recorder.recordRemoteAddress(remoteAddress); MessageDispatch md = (MessageDispatch) args[0]; ActiveMQMessage message = (ActiveMQMessage) md.getMessage(); ActiveMQDestination destination = message.getDestination(); // Rpc name is the URI of the queue/topic we're consuming from. recorder.recordRpcName(destination.getQualifiedName()); // Record acceptor host as the queue/topic name in order to generate virtual queue node. recorder.recordAcceptorHost(destination.getPhysicalName()); String parentApplicationName = ActiveMQClientHeader.getParentApplicationName(message, null); if (!recorder.isRoot() && parentApplicationName != null) { short parentApplicationType = ActiveMQClientHeader.getParentApplicationType(message, ServiceType.UNDEFINED.getCode()); recorder.recordParentApplication(parentApplicationName, parentApplicationType); } }
7. JMSConsumer6Test#testPassMessageListenerIntoCreateConsumer()
Project: activemq-artemis
File: JMSConsumer6Test.java
File: JMSConsumer6Test.java
@Test public void testPassMessageListenerIntoCreateConsumer() throws Exception { final AtomicInteger counter = new AtomicInteger(0); final CountDownLatch done = new CountDownLatch(1); // Receive a message with the JMS API connection.start(); ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQDestination destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination, new MessageListener() { @Override public void onMessage(Message m) { counter.incrementAndGet(); if (counter.get() == 4) { done.countDown(); } } }); assertNotNull(consumer); // Send the messages sendMessages(session, destination, 4); assertTrue(done.await(1000, TimeUnit.MILLISECONDS)); Thread.sleep(200); // Make sure only 4 messages were delivered. assertEquals(4, counter.get()); }
8. FTPBlobTest#testBlobFile()
Project: activemq-artemis
File: FTPBlobTest.java
File: FTPBlobTest.java
public void testBlobFile() throws Exception { setConnection(); // first create Message File file = File.createTempFile("amq-data-file-", ".dat"); // lets write some data String content = "hello world " + System.currentTimeMillis(); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.append(content); writer.close(); ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(destination); MessageConsumer consumer = session.createConsumer(destination); BlobMessage message = session.createBlobMessage(file); message.setName("fileName"); producer.send(message); Thread.sleep(1000); // check message send Message msg = consumer.receive(1000); assertTrue(msg instanceof ActiveMQBlobMessage); assertEquals("name is correct", "fileName", ((ActiveMQBlobMessage) msg).getName()); InputStream input = ((ActiveMQBlobMessage) msg).getInputStream(); StringBuilder b = new StringBuilder(); int i = input.read(); while (i != -1) { b.append((char) i); i = input.read(); } input.close(); File uploaded = new File(ftpHomeDirFile, msg.getJMSMessageID().toString().replace(":", "_")); assertEquals(content, b.toString()); assertTrue(uploaded.exists()); ((ActiveMQBlobMessage) msg).deleteFile(); assertFalse(uploaded.exists()); }
9. FilesystemBlobTest#testBlobFile()
Project: activemq-artemis
File: FilesystemBlobTest.java
File: FilesystemBlobTest.java
public void testBlobFile() throws Exception { // first create Message File file = File.createTempFile("amq-data-file-", ".dat"); // lets write some data String content = "hello world " + System.currentTimeMillis(); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.append(content); writer.close(); ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(destination); MessageConsumer consumer = session.createConsumer(destination); BlobMessage message = session.createBlobMessage(file); producer.send(message); Thread.sleep(1000); // check message send Message msg = consumer.receive(1000); assertTrue(msg instanceof ActiveMQBlobMessage); InputStream input = ((ActiveMQBlobMessage) msg).getInputStream(); StringBuilder b = new StringBuilder(); int i = input.read(); while (i != -1) { b.append((char) i); i = input.read(); } input.close(); File uploaded = new File(tmpDir, msg.getJMSMessageID().toString().replace(":", "_")); assertEquals(content, b.toString()); assertTrue(uploaded.exists()); ((ActiveMQBlobMessage) msg).deleteFile(); assertFalse(uploaded.exists()); }