Search in sources :

Example 56 with JMSContext

use of javax.jms.JMSContext in project Payara by payara.

the class AbstractJMSContextManager method cleanup.

// Close and remove the JMSContext instances
@PreDestroy
public synchronized void cleanup() {
    ServiceLocator serviceLocator = Globals.get(ServiceLocator.class);
    InvocationManager invMgr = serviceLocator.getService(InvocationManager.class);
    ComponentInvocation currentInv = invMgr.getCurrentInvocation();
    for (Entry<String, JMSContextEntry> entry : contexts.entrySet()) {
        JMSContextEntry contextEntry = entry.getValue();
        String ipId = contextEntry.getInjectionPointId();
        JMSContext context = contextEntry.getCtx();
        if (context != null) {
            ComponentInvocation inv = contextEntry.getComponentInvocation();
            if (inv != null && currentInv != inv)
                invMgr.preInvoke(inv);
            try {
                context.close();
                if (logger.isLoggable(Level.FINE)) {
                    logger.log(Level.FINE, localStrings.getLocalString("JMSContext.impl.close", "Closed JMSContext instance associated with id {0}: {1}.", ipId, context.toString()));
                }
            } catch (Exception e) {
                logger.log(Level.SEVERE, localStrings.getLocalString("JMSContext.impl.close.failure", "Failed to close JMSContext instance associated with id {0}: {1}.", ipId, context.toString()), e);
            } finally {
                if (inv != null && currentInv != inv)
                    invMgr.postInvoke(inv);
            }
        }
    }
    contexts.clear();
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation) InvocationManager(org.glassfish.api.invocation.InvocationManager) JMSContext(javax.jms.JMSContext) PreDestroy(javax.annotation.PreDestroy)

Example 57 with JMSContext

use of javax.jms.JMSContext in project Payara by payara.

the class AbstractJMSContextManager method createContext.

protected JMSContext createContext(String ipId, JMSContextMetadata metadata, ConnectionFactory connectionFactory) {
    int sessionMode = metadata.getSessionMode();
    String userName = metadata.getUserName();
    JMSContext context = null;
    if (userName == null) {
        context = connectionFactory.createContext(sessionMode);
    } else {
        String password = metadata.getPassword();
        context = connectionFactory.createContext(userName, password, sessionMode);
    }
    if (logger.isLoggable(Level.FINE)) {
        logger.log(Level.FINE, localStrings.getLocalString("JMSContext.impl.create", "Created new JMSContext instance associated with id {0}: {1}.", ipId, context.toString()));
    }
    return context;
}
Also used : JMSContext(javax.jms.JMSContext)

Example 58 with JMSContext

use of javax.jms.JMSContext in project Payara by payara.

the class AbstractJMSContextManager method getContext.

public synchronized JMSContext getContext(String ipId, String id, JMSContextMetadata metadata, ConnectionFactory connectionFactory) {
    JMSContextEntry contextEntry = contexts.get(id);
    JMSContext context = null;
    if (contextEntry == null) {
        context = createContext(ipId, metadata, connectionFactory);
        ServiceLocator serviceLocator = Globals.get(ServiceLocator.class);
        InvocationManager invMgr = serviceLocator.getService(InvocationManager.class);
        contexts.put(id, new JMSContextEntry(ipId, context, invMgr.getCurrentInvocation()));
    } else {
        context = contextEntry.getCtx();
    }
    return context;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) InvocationManager(org.glassfish.api.invocation.InvocationManager) JMSContext(javax.jms.JMSContext)

Example 59 with JMSContext

use of javax.jms.JMSContext in project eap-additional-testsuite by jboss-set.

the class ExportImportJournalTestCase method receiveMessage.

protected static void receiveMessage(Context ctx, String destinationLookup, boolean expectReceivedMessage, String expectedText) throws NamingException {
    ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
    assertNotNull(cf);
    Destination destination = (Destination) ctx.lookup(destinationLookup);
    assertNotNull(destination);
    try (JMSContext context = cf.createContext("guest", "guest")) {
        JMSConsumer consumer = context.createConsumer(destination);
        String text = consumer.receiveBody(String.class, 5000);
        if (expectReceivedMessage) {
            assertNotNull(text);
            assertEquals(expectedText, text);
        } else {
            assertNull("should not have received any message", text);
        }
    }
}
Also used : Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSContext(javax.jms.JMSContext)

Example 60 with JMSContext

use of javax.jms.JMSContext in project eap-additional-testsuite by jboss-set.

the class ExportImportJournalTestCase method sendMessage.

protected static void sendMessage(Context ctx, String destinationLookup, String text) throws NamingException, JMSException {
    ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
    assertNotNull(cf);
    Destination destination = (Destination) ctx.lookup(destinationLookup);
    assertNotNull(destination);
    try (JMSContext context = cf.createContext("guest", "guest")) {
        TextMessage message = context.createTextMessage(text);
        message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
        context.createProducer().send(destination, message);
    }
}
Also used : Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage)

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