Search in sources :

Example 86 with JMSContext

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

the class DeserializationMessagingBean method send.

public void send(Serializable serializable) throws NamingException {
    assertNotNull(queue);
    Context namingContext = new InitialContext();
    ConnectionFactory cf = (ConnectionFactory) namingContext.lookup(BLACK_LIST_CF_LOOKUP);
    assertNotNull(cf);
    try (JMSContext context = cf.createContext(JMSContext.AUTO_ACKNOWLEDGE)) {
        context.createProducer().send(queue, serializable);
    }
}
Also used : InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext) Context(javax.naming.Context) ConnectionFactory(javax.jms.ConnectionFactory) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext)

Example 87 with JMSContext

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

the class DeserializationMessagingBean method receive.

public void receive(Serializable serializable, String cfLookup, boolean consumeMustFail) throws NamingException {
    assertNotNull(queue);
    Context namingContext = new InitialContext();
    ConnectionFactory cf = (ConnectionFactory) namingContext.lookup(cfLookup);
    try (JMSContext context = cf.createContext(JMSContext.AUTO_ACKNOWLEDGE)) {
        JMSConsumer consumer = context.createConsumer(queue);
        try {
            Message response = consumer.receive(1000);
            assertNotNull(response);
            assertTrue(response instanceof ObjectMessage);
            Serializable o = ((ObjectMessage) response).getObject();
            if (consumeMustFail) {
                fail(o + " must not be deserialized when message is consumed");
            } else {
                assertEquals(serializable, o);
            }
        } catch (JMSException e) {
            if (consumeMustFail) {
                log.trace("Expected JMSException", e);
            } else {
                log.error("Unexpected exception", e);
                fail(serializable + " is allowed to be deserialized when message is consumed");
            }
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext) Context(javax.naming.Context) ConnectionFactory(javax.jms.ConnectionFactory) Serializable(java.io.Serializable) JMSConsumer(javax.jms.JMSConsumer) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext)

Example 88 with JMSContext

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

the class AbstractSaslTestBase method sendAndReceiveMsg.

protected void sendAndReceiveMsg(int remotingPort, boolean expectedSaslFail, String username, String password) {
    Context namingContext = null;
    try {
        // Set up the namingContext for the JNDI lookup
        final Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, WildFlyInitialContextFactory.class.getName());
        env.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
        env.put(Context.PROVIDER_URL, "remote://" + HOST_FMT + ":" + remotingPort);
        namingContext = new InitialContext(env);
        // Perform the JNDI lookups
        ConnectionFactory connectionFactory = null;
        try {
            connectionFactory = (ConnectionFactory) namingContext.lookup(CONNECTION_FACTORY);
            assertFalse("JNDI lookup should have failed.", expectedSaslFail);
        } catch (NamingException e) {
            if (expectedSaslFail) {
                // only SASL failures are expected
                assertTrue("Unexpected cause of lookup failure", e.getCause() instanceof SaslException);
                return;
            }
            throw e;
        }
        Destination destination = (Destination) namingContext.lookup(NAME);
        try (JMSContext context = (username != null ? connectionFactory.createContext(username, password) : connectionFactory.createContext())) {
            // Send a message
            context.createProducer().send(destination, MESSAGE);
            // Create the JMS consumer
            JMSConsumer consumer = context.createConsumer(destination);
            // Then receive the same message that was sent
            String text = consumer.receiveBody(String.class, TimeoutUtil.adjust(5000));
            Assert.assertEquals(MESSAGE, text);
        }
    } catch (NamingException e) {
        LOGGER.error("Naming problem occured.", e);
        throw new RuntimeException(e);
    } finally {
        if (namingContext != null) {
            try {
                namingContext.close();
            } catch (NamingException e) {
                LOGGER.error("Naming problem occured during closing context.", e);
            }
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext) Context(javax.naming.Context) WildFlyInitialContextFactory(org.wildfly.naming.client.WildFlyInitialContextFactory) Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) NamingException(javax.naming.NamingException) Properties(java.util.Properties) SaslException(javax.security.sasl.SaslException) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext)

Example 89 with JMSContext

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

the class ClusteredMessagingTestCase method testClusteredTopic.

@Test
public void testClusteredTopic() throws Exception {
    InitialContext contextFromServer1 = createJNDIContext(NODE_1);
    InitialContext contextFromServer2 = createJNDIContext(NODE_2);
    try (JMSContext jmsContext1 = createJMSContext(createJNDIContext(NODE_1));
        JMSConsumer consumer1 = jmsContext1.createConsumer((Destination) contextFromServer1.lookup(jmsTopicLookup));
        JMSContext jmsContext2 = createJMSContext(createJNDIContext(NODE_2));
        JMSConsumer consumer2 = jmsContext2.createConsumer((Destination) contextFromServer2.lookup(jmsTopicLookup))) {
        String text = UUID.randomUUID().toString();
        Destination destination1 = (Destination) contextFromServer1.lookup(jmsTopicLookup);
        Destination destination2 = (Destination) contextFromServer2.lookup(jmsTopicLookup);
        // send a message to the topic on server 1
        sendMessage(contextFromServer1, destination1, text);
        // consumers receive it on both servers
        receiveMessage(consumer1, text);
        receiveMessage(consumer2, text);
        String anotherText = UUID.randomUUID().toString();
        // send another message to topic on server 2
        sendMessage(contextFromServer2, destination2, anotherText);
        // consumers receive it on both servers
        receiveMessage(consumer1, anotherText);
        receiveMessage(consumer2, anotherText);
    } finally {
        contextFromServer1.close();
        contextFromServer2.close();
    }
}
Also used : Destination(javax.jms.Destination) JMSConsumer(javax.jms.JMSConsumer) InitialContext(javax.naming.InitialContext) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 90 with JMSContext

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

the class ClusteredMessagingTestCase method sendMessage.

protected static void sendMessage(Context ctx, Destination destination, String text) throws NamingException {
    ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
    assertNotNull(cf);
    assertNotNull(destination);
    try (JMSContext context = cf.createContext(JMS_USERNAME, JMS_PASSWORD)) {
        context.createProducer().send(destination, text);
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) 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