Search in sources :

Example 26 with JMSProducer

use of javax.jms.JMSProducer in project activemq-artemis by apache.

the class JmsContextTest method testRollbackTest.

@Test
public void testRollbackTest() {
    JMSContext ctx = addContext(cf.createContext(JMSContext.SESSION_TRANSACTED));
    JMSProducer producer = ctx.createProducer();
    JMSConsumer cons = ctx.createConsumer(queue1);
    producer.send(queue1, context.createTextMessage("hello"));
    ctx.rollback();
    assertNull(cons.receiveNoWait());
    producer.send(queue1, context.createTextMessage("hello"));
    ctx.commit();
    assertNotNull(cons.receiveNoWait());
    ctx.commit();
    ctx.rollback();
    assertNull(cons.receiveNoWait());
    cons.close();
}
Also used : JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 27 with JMSProducer

use of javax.jms.JMSProducer in project activemq-artemis by apache.

the class JmsContextTest method testExpire.

@Test
public void testExpire() throws Exception {
    JMSProducer producer = context.createProducer();
    producer.setTimeToLive(500);
    String strRandom = newXID().toString();
    producer.send(queue1, context.createTextMessage(strRandom));
    Thread.sleep(700);
    // Create consumer after message is expired, making it to expire at the server's
    JMSConsumer consumer = context.createConsumer(queue1);
    TextMessage msg = (TextMessage) consumer.receiveNoWait();
    // Time to live kicked in, so it's supposed to return null
    assertNull(msg);
    strRandom = newXID().toString();
    producer.send(queue1, context.createTextMessage(strRandom));
    Thread.sleep(700);
    // Receive second message, expiring on client
    msg = (TextMessage) consumer.receiveNoWait();
    assertNull(msg);
    strRandom = newXID().toString();
    producer.send(queue1, context.createTextMessage(strRandom));
    // will receive a message that's not expired now
    msg = (TextMessage) consumer.receiveNoWait();
    assertNotNull(msg);
    assertEquals(strRandom, msg.getText());
}
Also used : JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 28 with JMSProducer

use of javax.jms.JMSProducer in project activemq-artemis by apache.

the class JmsContextTest method testSendStreamMessage.

@Test
public void testSendStreamMessage() throws JMSException, InterruptedException {
    JmsProducerCompletionListenerTest.CountingCompletionListener cl = new JmsProducerCompletionListenerTest.CountingCompletionListener(1);
    JMSProducer producer = context.createProducer();
    producer.setAsync(cl);
    StreamMessage msg = context.createStreamMessage();
    msg.setStringProperty("name", name.getMethodName());
    String bprop = "booleanProp";
    String iprop = "intProp";
    msg.setBooleanProperty(bprop, true);
    msg.setIntProperty(iprop, 42);
    msg.writeBoolean(true);
    msg.writeInt(67);
    producer.send(queue1, msg);
    JMSConsumer consumer = context.createConsumer(queue1);
    Message msg2 = consumer.receive(100);
    Assert.assertNotNull(msg2);
    Assert.assertTrue(cl.completionLatch.await(1, TimeUnit.SECONDS));
    StreamMessage sm = (StreamMessage) cl.lastMessage;
    Assert.assertEquals(true, sm.getBooleanProperty(bprop));
    Assert.assertEquals(42, sm.getIntProperty(iprop));
    Assert.assertEquals(true, sm.readBoolean());
    Assert.assertEquals(67, sm.readInt());
}
Also used : JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) JMSProducer(javax.jms.JMSProducer) StreamMessage(javax.jms.StreamMessage) Test(org.junit.Test)

Example 29 with JMSProducer

use of javax.jms.JMSProducer in project activemq-artemis by apache.

the class JmsContextTest method testInvalidMessage.

@Test
public void testInvalidMessage() {
    JMSProducer producer = context.createProducer();
    try {
        producer.send(queue1, (Message) null);
        Assert.fail("null msg");
    } catch (MessageFormatRuntimeException expected) {
    // no-op
    }
}
Also used : JMSProducer(javax.jms.JMSProducer) MessageFormatRuntimeException(javax.jms.MessageFormatRuntimeException) Test(org.junit.Test)

Example 30 with JMSProducer

use of javax.jms.JMSProducer in project activemq-artemis by apache.

the class JmsContextTest method testSetClientIdLate.

@Test
public void testSetClientIdLate() {
    JMSProducer producer = context.createProducer();
    Message msg = context.createMessage();
    producer.send(queue1, msg);
    try {
        context.setClientID("id");
        Assert.fail("expected exception");
    } catch (IllegalStateRuntimeException e) {
    // no op
    }
}
Also used : IllegalStateRuntimeException(javax.jms.IllegalStateRuntimeException) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) JMSProducer(javax.jms.JMSProducer) Test(org.junit.Test)

Aggregations

JMSProducer (javax.jms.JMSProducer)39 Test (org.junit.Test)32 JMSContext (javax.jms.JMSContext)22 JMSConsumer (javax.jms.JMSConsumer)21 TextMessage (javax.jms.TextMessage)20 Message (javax.jms.Message)11 BytesMessage (javax.jms.BytesMessage)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)8 ConnectionFactory (javax.jms.ConnectionFactory)7 Destination (javax.jms.Destination)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 StreamMessage (javax.jms.StreamMessage)6 JMSException (javax.jms.JMSException)5 JMSRuntimeException (javax.jms.JMSRuntimeException)4 Queue (javax.jms.Queue)4 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)4 Topic (javax.jms.Topic)3 CompletionListener (javax.jms.CompletionListener)2 IllegalStateRuntimeException (javax.jms.IllegalStateRuntimeException)2 InvalidDestinationRuntimeException (javax.jms.InvalidDestinationRuntimeException)2