use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class JmsContextTest method illegalStateRuntimeExceptionTests.
@Test
public void illegalStateRuntimeExceptionTests() throws Exception {
JMSProducer producer = context.createProducer();
JMSConsumer consumer = context.createConsumer(queue1);
System.out.println("Creating TextMessage");
TextMessage expTextMessage = context.createTextMessage("Call commit");
CountDownLatch latch = new CountDownLatch(1);
JMSCOntextStopCompletionListener listener = new JMSCOntextStopCompletionListener(context, latch);
producer.setAsync(listener);
producer.send(queue1, expTextMessage);
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertNull(listener.ex);
}
use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class JmsContextTest method testContextStopAndCloseFromMessageListeners.
@Test
public void testContextStopAndCloseFromMessageListeners() throws Exception {
final JMSContext context1 = context.createContext(Session.AUTO_ACKNOWLEDGE);
JMSConsumer consumer1 = context1.createConsumer(queue1);
final CountDownLatch latch1 = new CountDownLatch(1);
InvalidMessageListener listener1 = new InvalidMessageListener(context1, latch1, 1);
consumer1.setMessageListener(listener1);
JMSProducer producer = context1.createProducer();
Message msg = context1.createTextMessage("first message");
producer.send(queue1, msg);
latch1.await();
Throwable error1 = listener1.getError();
assertNotNull(error1);
assertTrue(error1 instanceof IllegalStateRuntimeException);
context1.close();
final JMSContext context2 = context.createContext(Session.AUTO_ACKNOWLEDGE);
JMSConsumer consumer2 = context2.createConsumer(queue1);
final CountDownLatch latch2 = new CountDownLatch(1);
InvalidMessageListener listener2 = new InvalidMessageListener(context2, latch2, 2);
consumer2.setMessageListener(listener2);
JMSProducer producer2 = context2.createProducer();
Message msg2 = context2.createTextMessage("second message");
producer2.send(queue1, msg2);
latch2.await();
Throwable error2 = listener2.getError();
assertNotNull(error2);
assertTrue(error2 instanceof IllegalStateRuntimeException);
context2.close();
}
use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class JmsContextTest method testCloseSecondContextConnectionRemainsOpen.
@Test
public void testCloseSecondContextConnectionRemainsOpen() throws JMSException {
JMSContext localContext = context.createContext(JMSContext.CLIENT_ACKNOWLEDGE);
Assert.assertEquals("client_ack", JMSContext.CLIENT_ACKNOWLEDGE, localContext.getSessionMode());
JMSProducer producer = localContext.createProducer();
JMSConsumer consumer = localContext.createConsumer(queue1);
final int pass = 1;
for (int idx = 0; idx < 2; idx++) {
Message m = localContext.createMessage();
int intProperty = random.nextInt();
m.setIntProperty("random", intProperty);
Assert.assertNotNull(m);
producer.send(queue1, m);
m = null;
Message msg = consumer.receive(100);
Assert.assertNotNull("must have a msg", msg);
Assert.assertEquals(intProperty, msg.getIntProperty("random"));
/* In the second pass we close the connection before ack'ing */
if (idx == pass) {
localContext.close();
}
/**
* From {@code JMSContext.close()}'s javadoc:<br/>
* Invoking the {@code acknowledge} method of a received message from a closed connection's
* session must throw an {@code IllegalStateRuntimeException}. Closing a closed connection
* must NOT throw an exception.
*/
try {
msg.acknowledge();
Assert.assertEquals("connection should be open on pass 0. It is " + pass, 0, idx);
} catch (javax.jms.IllegalStateException expected) {
// HORNETQ-1209 "JMS 2.0" XXX JMSContext javadoc says we must expect a
// IllegalStateRuntimeException here. But Message.ack...() says it must throws the
// non-runtime variant.
Assert.assertEquals("we only close the connection on pass " + pass, pass, idx);
}
}
}
use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class SharedConsumerTest method sharedDurableSubSimpleRoundRobin.
@Test
public void sharedDurableSubSimpleRoundRobin() throws Exception {
context = cf.createContext();
try {
JMSConsumer con1 = context.createSharedDurableConsumer(topic1, "mySharedCon");
JMSConsumer con2 = context.createSharedDurableConsumer(topic1, "mySharedCon");
context.start();
JMSProducer producer = context.createProducer();
int numMessages = 10;
for (int i = 0; i < numMessages; i++) {
producer.send(topic1, "msg:" + i);
}
for (int i = 0; i < numMessages; i += 2) {
String msg = con1.receiveBody(String.class, 5000);
System.out.println("msg = " + msg);
msg = con2.receiveBody(String.class, 5000);
System.out.println("msg = " + msg);
}
} finally {
context.close();
}
}
use of javax.jms.JMSProducer in project activemq-artemis by apache.
the class JMSCompletionListenerExample method main.
public static void main(final String[] args) throws Exception {
JMSContext jmsContext = null;
try {
// Step 2. Perfom a lookup on the queue
Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
// Step 3. Perform a lookup on the Connection Factory
ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616?confirmationWindowSize=10240");
// Step 4.Create a JMS Context
jmsContext = cf.createContext();
// Step 5. Create a message producer.
JMSProducer producer = jmsContext.createProducer();
final CountDownLatch latch = new CountDownLatch(1);
// Step 6. We want to send the message Asynchronously and be notified when the Broker receives it so we set a completion handler
producer.setAsync(new CompletionListener() {
@Override
public void onCompletion(Message message) {
System.out.println("message acknowledged by ActiveMQ");
latch.countDown();
}
@Override
public void onException(Message message, Exception e) {
e.printStackTrace();
}
});
// Step 6. Send the Message
producer.send(queue, "this is a string");
// Step 7. wait for the Completion handler
if (!latch.await(5, TimeUnit.SECONDS)) {
throw new IllegalStateException("Completion listener not called as expected.");
}
} finally {
if (jmsContext != null) {
jmsContext.close();
}
}
}
Aggregations