Search in sources :

Example 51 with JMSContext

use of javax.jms.JMSContext in project brave by openzipkin.

the class ITJms_2_0_TracingMessageConsumer method receive_customSampler.

@Test
public void receive_customSampler() throws JMSException {
    queueReceiver.close();
    MessagingRuleSampler consumerSampler = MessagingRuleSampler.newBuilder().putRule(channelNameEquals(jms.queue.getQueueName()), Sampler.NEVER_SAMPLE).build();
    try (MessagingTracing messagingTracing = MessagingTracing.newBuilder(tracing).consumerSampler(consumerSampler).build();
        JMSContext context = JmsTracing.create(messagingTracing).connectionFactory(((ArtemisJmsTestRule) jms).factory).createContext(JMSContext.AUTO_ACKNOWLEDGE);
        JMSConsumer consumer = context.createConsumer(jms.queue)) {
        queueSender.send(message);
        // Check that the message headers are not sampled
        assertThat(consumer.receive().getStringProperty("b3")).endsWith("-0");
    }
// @After will also check that the consumer was not sampled
}
Also used : JMSConsumer(javax.jms.JMSConsumer) MessagingTracing(brave.messaging.MessagingTracing) MessagingRuleSampler(brave.messaging.MessagingRuleSampler) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 52 with JMSContext

use of javax.jms.JMSContext in project brave by openzipkin.

the class ITJms_2_0_TracingMessageProducer method customSampler.

@Test
public void customSampler() throws JMSException {
    MessagingRuleSampler producerSampler = MessagingRuleSampler.newBuilder().putRule(channelNameEquals(jms.queue.getQueueName()), Sampler.NEVER_SAMPLE).build();
    try (MessagingTracing messagingTracing = MessagingTracing.newBuilder(tracing).producerSampler(producerSampler).build();
        JMSContext context = JmsTracing.create(messagingTracing).connectionFactory(((ArtemisJmsTestRule) jms).factory).createContext(JMSContext.AUTO_ACKNOWLEDGE)) {
        context.createProducer().send(jms.queue, "foo");
    }
    Message received = queueReceiver.receive();
    assertThat(propertiesToMap(received)).containsKey("b3").satisfies(m -> assertThat(m.get("b3")).endsWith("-0"));
// @After will also check that the producer was not sampled
}
Also used : Message(javax.jms.Message) MessagingTracing(brave.messaging.MessagingTracing) MessagingRuleSampler(brave.messaging.MessagingRuleSampler) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 53 with JMSContext

use of javax.jms.JMSContext in project qpid-broker-j by apache.

the class DeliveryDelayTest method testDeliveryDelayNotSupportedByQueueViaExchange_MessageRejected.

/**
 * The client sends a messagge to a fanout exchange instance which is bound to a queue with
 * holdsOnPublish turned off. The Broker must reject the message.
 */
@Test
public void testDeliveryDelayNotSupportedByQueueViaExchange_MessageRejected() throws Exception {
    try (JMSContext context = getConnectionBuilder().buildConnectionFactory().createContext()) {
        String testQueueName = BrokerAdmin.TEST_QUEUE_NAME;
        String testExchangeName = "test_exch";
        Destination consumeDest = createQueue(context, testQueueName, false);
        Destination publishDest = createExchange(context, testExchangeName);
        bindQueueToExchange(testExchangeName, testQueueName);
        JMSConsumer consumer = context.createConsumer(consumeDest);
        JMSProducer producer = context.createProducer();
        producer.send(publishDest, "message without delivery delay");
        Message message = consumer.receive(getReceiveTimeout());
        assertNotNull("Message published without delivery delay not received", message);
        producer.setDeliveryDelay(DELIVERY_DELAY);
        try {
            producer.send(publishDest, "message with delivery delay");
            fail("Exception not thrown");
        } catch (JMSRuntimeException e) {
            assertTrue("Unexpected exception message: " + e.getMessage(), e.getMessage().contains("amqp:precondition-failed"));
        }
    }
}
Also used : Destination(javax.jms.Destination) JMSConsumer(javax.jms.JMSConsumer) Message(javax.jms.Message) JMSProducer(javax.jms.JMSProducer) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 54 with JMSContext

use of javax.jms.JMSContext in project qpid-broker-j by apache.

the class DeliveryDelayTest method testDeliveryDelayNotSupportedByQueue_MessageRejected.

/**
 * The target queue, which is addressed directly by the client, does not have
 * holdsOnPublish turned on.  The Broker must reject the message.
 */
@Test
public void testDeliveryDelayNotSupportedByQueue_MessageRejected() throws Exception {
    try (JMSContext context = getConnectionBuilder().buildConnectionFactory().createContext()) {
        Destination queue = createQueue(context, BrokerAdmin.TEST_QUEUE_NAME, false);
        JMSProducer producer = context.createProducer().setDeliveryDelay(DELIVERY_DELAY);
        try {
            producer.send(queue, "message");
            fail("Exception not thrown");
        } catch (JMSRuntimeException e) {
            assertTrue("Unexpected exception message: " + e.getMessage(), e.getMessage().contains("amqp:precondition-failed"));
        }
    }
}
Also used : Destination(javax.jms.Destination) JMSProducer(javax.jms.JMSProducer) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 55 with JMSContext

use of javax.jms.JMSContext in project quickstart by wildfly.

the class JMSRaceStage method run.

@Override
public void run(Race.Registration registration) throws Exception {
    try (JMSContext jmsContext = cf.createContext()) {
        // create response tmp queue
        final TemporaryQueue responseQueue = jmsContext.createTemporaryQueue();
        // send request
        final String request = UUID.randomUUID().toString();
        jmsContext.createProducer().setJMSReplyTo(responseQueue).send(requestQueue, request);
        // receive response
        try (JMSConsumer consumer = jmsContext.createConsumer(responseQueue)) {
            String response = consumer.receiveBody(String.class);
            if (response == null) {
                registration.aborted(new IllegalStateException("Message processing timed out"));
            } else if (!response.equals(request)) {
                registration.aborted(new IllegalStateException("Response content does not match the request. Response: " + response + ", request: " + request));
            }
        }
    }
}
Also used : JMSConsumer(javax.jms.JMSConsumer) TemporaryQueue(javax.jms.TemporaryQueue) JMSContext(javax.jms.JMSContext)

Aggregations

JMSContext (javax.jms.JMSContext)120 Test (org.junit.Test)57 JMSConsumer (javax.jms.JMSConsumer)44 ConnectionFactory (javax.jms.ConnectionFactory)35 Destination (javax.jms.Destination)30 TextMessage (javax.jms.TextMessage)28 JMSProducer (javax.jms.JMSProducer)27 Message (javax.jms.Message)18 JMSRuntimeException (javax.jms.JMSRuntimeException)13 TemporaryQueue (javax.jms.TemporaryQueue)13 InitialContext (javax.naming.InitialContext)12 JMSException (javax.jms.JMSException)10 Queue (javax.jms.Queue)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)7 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 QueueConnectionFactory (javax.jms.QueueConnectionFactory)4 Context (javax.naming.Context)4 NamingException (javax.naming.NamingException)4