Search in sources :

Example 6 with JMSProducer

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

the class JMSSharedConsumerExample method main.

public static void main(final String[] args) throws Exception {
    InitialContext initialContext = null;
    JMSContext jmsContext = null;
    JMSContext jmsContext2 = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Perfom a lookup on the queue
        Topic topic = (Topic) initialContext.lookup("topic/exampleTopic");
        // Step 3. Perform a lookup on the Connection Factory
        ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 4.Create a JMS Context
        jmsContext = cf.createContext();
        // Step 5. Create a message producer.
        JMSProducer producer = jmsContext.createProducer();
        // Step 6. Create a shared consumer
        JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(topic, "sc1");
        // Step 7. Create a second JMS Context for a second shared consumer
        jmsContext2 = cf.createContext();
        // Step 8. Create the second shared consumer
        JMSConsumer jmsConsumer2 = jmsContext2.createSharedConsumer(topic, "sc1");
        // Step 9. send 2 messages
        producer.send(topic, "this is a String!");
        producer.send(topic, "this is a second String!");
        // Step 10. receive the messages shared by both consumers
        String body = jmsConsumer.receiveBody(String.class, 5000);
        System.out.println("body = " + body);
        body = jmsConsumer2.receiveBody(String.class, 5000);
        System.out.println("body = " + body);
    } finally {
        // Step 11. Be sure to close our JMS resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (jmsContext != null) {
            jmsContext.close();
        }
        if (jmsContext2 != null) {
            jmsContext2.close();
        }
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) Topic(javax.jms.Topic) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext)

Example 7 with JMSProducer

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

the class JmsConsumerTest method testShareDuraleWithJMSContext.

@Test
public void testShareDuraleWithJMSContext() throws Exception {
    ((ActiveMQConnectionFactory) cf).setConsumerWindowSize(0);
    JMSContext conn = cf.createContext(JMSContext.AUTO_ACKNOWLEDGE);
    JMSConsumer consumer = conn.createSharedDurableConsumer(topic, "c1");
    JMSProducer producer = conn.createProducer();
    for (int i = 0; i < 100; i++) {
        producer.setProperty("count", i).send(topic, "test" + i);
    }
    JMSContext conn2 = conn.createContext(JMSContext.AUTO_ACKNOWLEDGE);
    JMSConsumer consumer2 = conn2.createSharedDurableConsumer(topic, "c1");
    for (int i = 0; i < 50; i++) {
        String txt = consumer.receiveBody(String.class, 5000);
        System.out.println("TXT:" + txt);
        Assert.assertNotNull(txt);
        txt = consumer.receiveBody(String.class, 5000);
        System.out.println("TXT:" + txt);
        Assert.assertNotNull(txt);
    }
    Assert.assertNull(consumer.receiveNoWait());
    Assert.assertNull(consumer2.receiveNoWait());
    boolean exceptionHappened = false;
    try {
        conn.unsubscribe("c1");
    } catch (Exception e) {
        e.printStackTrace();
        exceptionHappened = true;
    }
    Assert.assertTrue(exceptionHappened);
    consumer.close();
    consumer2.close();
    conn2.close();
    conn.unsubscribe("c1");
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSContext(javax.jms.JMSContext) JMSException(javax.jms.JMSException) Test(org.junit.Test)

Example 8 with JMSProducer

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

the class JmsContextTest method testReceiveBytes.

@Test
public void testReceiveBytes() throws Exception {
    JMSProducer producer = context.createProducer();
    JMSConsumer consumer = context.createConsumer(queue1);
    BytesMessage bytesSend = context.createBytesMessage();
    bytesSend.writeByte((byte) 1);
    bytesSend.writeLong(2L);
    producer.send(queue1, bytesSend);
    BytesMessage msgReceived = (BytesMessage) consumer.receiveNoWait();
    byte[] bytesArray = msgReceived.getBody(byte[].class);
    assertEquals((byte) 1, msgReceived.readByte());
    assertEquals(2L, msgReceived.readLong());
    DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(bytesArray));
    assertEquals((byte) 1, dataInputStream.readByte());
    assertEquals(2L, dataInputStream.readLong());
}
Also used : JMSConsumer(javax.jms.JMSConsumer) ByteArrayInputStream(java.io.ByteArrayInputStream) JMSProducer(javax.jms.JMSProducer) BytesMessage(javax.jms.BytesMessage) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 9 with JMSProducer

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

the class JmsContextTest method bytesMessage.

@Test
public void bytesMessage() throws Exception {
    context = cf.createContext();
    try {
        JMSProducer producer = context.createProducer();
        BytesMessage bMsg = context.createBytesMessage();
        bMsg.setStringProperty("COM_SUN_JMS_TESTNAME", "sendAndRecvMsgOfEachTypeCLTest");
        bMsg.writeByte((byte) 1);
        bMsg.writeInt(22);
        CountDownLatch latch = new CountDownLatch(1);
        SimpleCompletionListener listener = new SimpleCompletionListener(latch);
        producer.setAsync(listener);
        producer.send(queue1, bMsg);
        assertTrue(latch.await(5, TimeUnit.SECONDS));
        assertEquals(listener.message.readByte(), (byte) 1);
        assertEquals(listener.message.readInt(), 22);
    } finally {
        context.close();
    }
}
Also used : JMSProducer(javax.jms.JMSProducer) BytesMessage(javax.jms.BytesMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with JMSProducer

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

the class JmsContextTest method testCreateConsumerWithSelector.

@Test
public void testCreateConsumerWithSelector() throws JMSException {
    final String filterName = "magicIndexMessage";
    final int total = 5;
    JMSProducer producer = context.createProducer();
    JMSConsumer consumerNoSelect = context.createConsumer(queue1);
    JMSConsumer consumer = context.createConsumer(queue1, filterName + "=TRUE");
    for (int i = 0; i < total; i++) {
        Message msg = context.createTextMessage("message " + i);
        msg.setBooleanProperty(filterName, i == 3);
        producer.send(queue1, msg);
    }
    Message msg0 = consumer.receive(500);
    Assert.assertNotNull(msg0);
    msg0.acknowledge();
    Assert.assertNull("no more messages", consumer.receiveNoWait());
    for (int i = 0; i < total - 1; i++) {
        Message msg = consumerNoSelect.receive(100);
        Assert.assertNotNull(msg);
        msg.acknowledge();
    }
    Assert.assertNull("no more messages", consumerNoSelect.receiveNoWait());
}
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) 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