Search in sources :

Example 26 with Queue

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

the class MessageDrivenTimeoutTestCase method noTimeout.

/**
     * MDB receives a message where <code>onMessage</code> method using an {@link XAResource} and adding transaction
     * synchronization. The processing should be finished with sucessful 2PC commit.
     */
@Test
public void noTimeout() throws Exception {
    String text = "no timeout";
    Queue q = sendMessage(text, TransactionTimeoutQueueSetupTask.NO_TIMEOUT_JNDI_NAME, initCtx);
    Assert.assertEquals("Sent and received message does not match at expected way", NoTimeoutMDB.REPLY_PREFIX + text, receiveMessage(q, initCtx));
    Assert.assertEquals("Synchronization before completion has to be called", 1, checker.countSynchronizedBefore());
    Assert.assertEquals("Synchronization after completion has to be called", 1, checker.countSynchronizedAfter());
    Assert.assertEquals("Expecting one test XA resources being commmitted", 1, checker.getCommitted());
    Assert.assertEquals("Expecting no rollback happened", 0, checker.getRolledback());
}
Also used : Queue(javax.jms.Queue) Test(org.junit.Test)

Example 27 with Queue

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

the class MessageDrivenTimeoutTestCase method transactionTimeoutActivationProperty.

/**
     * MDB receives a message MDB activation property is used to have transaction timeout defined.
     * MDB using {@link XAResource} but processing takes longer than and timeout time is exceeded.
     * As activation property instruct the RA to set transaction timeout then the transaction
     * should be rolled-back.
     */
@Test
public void transactionTimeoutActivationProperty() throws Exception {
    String text = "activation property timeout";
    Queue q = sendMessage(text, TransactionTimeoutQueueSetupTask.PROPERTY_TIMEOUT_JNDI_NAME, initCtx);
    Assert.assertNull("No message should be received as mdb timeouted", receiveMessage(q, initCtx));
    Assert.assertEquals("Expecting no commmit happened", 0, checker.getCommitted());
    Assert.assertTrue("Expecting a rollback happened", checker.getRolledback() > 0);
}
Also used : Queue(javax.jms.Queue) Test(org.junit.Test)

Example 28 with Queue

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

the class MessageDrivenTimeoutTestCase method transactionTimeoutAnnotation.

/**
     * MDB receives a message when annotated to have transaction timeout defined.
     * MDB using {@link XAResource} but processing takes longer than and timeout time is exceeded.
     * As transaction annotation does not affects MDB processing the commit should happen.
     */
@Test
public void transactionTimeoutAnnotation() throws Exception {
    String text = "annotation timeout";
    Queue q = sendMessage(text, TransactionTimeoutQueueSetupTask.ANNOTATION_TIMEOUT_JNDI_NAME, initCtx);
    Assert.assertEquals("Sent and received message does not match at expected way", AnnotationTimeoutMDB.REPLY_PREFIX + text, receiveMessage(q, initCtx));
    Assert.assertEquals("Expecting one test XA resources being commmitted", 1, checker.getCommitted());
    Assert.assertEquals("Expecting no rollback happened", 0, checker.getRolledback());
}
Also used : Queue(javax.jms.Queue) Test(org.junit.Test)

Example 29 with Queue

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

the class MessageDrivenTimeoutTestCase method sendMessage.

static Queue sendMessage(String text, String queueJndi, InitialContext initCtx) throws Exception {
    QueueConnection connection = getConnection(initCtx);
    connection.start();
    Queue replyDestination = null;
    try {
        final QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        final Message message = session.createTextMessage(text);
        replyDestination = (Queue) initCtx.lookup(TransactionTimeoutQueueSetupTask.REPLY_QUEUE_JNDI_NAME);
        message.setJMSReplyTo(replyDestination);
        final Destination destination = (Destination) initCtx.lookup(queueJndi);
        final MessageProducer producer = session.createProducer(destination);
        producer.send(message);
        producer.close();
    } finally {
        connection.close();
    }
    return replyDestination;
}
Also used : Destination(javax.jms.Destination) QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 30 with Queue

use of javax.jms.Queue in project tomee by apache.

the class AutoConfig method resolveDestinationLinks.

/**
     * Set resource id in all message-destination-refs and MDBs that are using message destination links.
     */
private void resolveDestinationLinks(final AppModule appModule) throws OpenEJBException {
    // build up a link resolver
    final LinkResolver<MessageDestination> destinationResolver = new LinkResolver<MessageDestination>();
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
        if (assembly != null) {
            for (final MessageDestination destination : assembly.getMessageDestination()) {
                destinationResolver.add(ejbModule.getModuleUri(), destination.getMessageDestinationName(), destination);
            }
        }
    }
    for (final ClientModule clientModule : appModule.getClientModules()) {
        for (final MessageDestination destination : clientModule.getApplicationClient().getMessageDestination()) {
            destinationResolver.add(appModule.getModuleUri(), destination.getMessageDestinationName(), destination);
        }
    }
    for (final WebModule webModule : appModule.getWebModules()) {
        for (final MessageDestination destination : webModule.getWebApp().getMessageDestination()) {
            destinationResolver.add(appModule.getModuleUri(), destination.getMessageDestinationName(), destination);
        }
    }
    // remember the type of each destination so we can use it to fillin MDBs that don't declare destination type
    final Map<MessageDestination, String> destinationTypes = new HashMap<MessageDestination, String>();
    // if MessageDestination does not have a mapped name assigned, give it the destination from the MDB
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
        if (assembly == null) {
            continue;
        }
        final URI moduleUri = ejbModule.getModuleUri();
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
            // MDB destination is deploymentId if none set
            if (bean instanceof MessageDrivenBean) {
                final MessageDrivenBean mdb = (MessageDrivenBean) bean;
                final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
                if (ejbDeployment == null) {
                    throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
                }
                // skip destination refs without a destination link
                final String link = mdb.getMessageDestinationLink();
                if (link == null || link.length() == 0) {
                    continue;
                }
                // resolve the destination... if we don't find one it is a configuration bug
                final MessageDestination destination = destinationResolver.resolveLink(link, moduleUri);
                if (destination == null) {
                    throw new OpenEJBException("Message destination " + link + " for message driven bean " + mdb.getEjbName() + " not found");
                }
                // get the destinationId is the mapped name
                String destinationId = destination.getMappedName();
                if (destinationId == null) {
                    // if we don't have a mapped name use the destination of the mdb
                    final Properties properties = mdb.getActivationConfig().toProperties();
                    destinationId = properties.getProperty("destination");
                    destination.setMappedName(destinationId);
                }
                if (mdb.getMessageDestinationType() != null && !destinationTypes.containsKey(destination)) {
                    destinationTypes.put(destination, mdb.getMessageDestinationType());
                }
                // destination identifier
                ResourceLink resourceLink = ejbDeployment.getResourceLink("openejb/destination");
                if (resourceLink == null) {
                    resourceLink = new ResourceLink();
                    resourceLink.setResRefName("openejb/destination");
                    ejbDeployment.addResourceLink(resourceLink);
                }
                resourceLink.setResId(destinationId);
            }
        }
    }
    // resolve all message destination refs with links and assign a ref id to the reference
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
        if (assembly == null) {
            continue;
        }
        final URI moduleUri = ejbModule.getModuleUri();
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
            final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
            if (ejbDeployment == null) {
                throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
            }
            for (final MessageDestinationRef ref : bean.getMessageDestinationRef()) {
                // skip destination refs with a resource link already assigned
                if (ref.getMappedName() == null && ejbDeployment.getResourceLink(ref.getName()) == null) {
                    final String destinationId = resolveDestinationId(ref, moduleUri, destinationResolver, destinationTypes);
                    if (destinationId != null) {
                        // build the link and add it
                        final ResourceLink resourceLink = new ResourceLink();
                        resourceLink.setResId(destinationId);
                        resourceLink.setResRefName(ref.getName());
                        ejbDeployment.addResourceLink(resourceLink);
                    }
                }
            }
        }
    }
    for (final ClientModule clientModule : appModule.getClientModules()) {
        final URI moduleUri = clientModule.getModuleUri();
        for (final MessageDestinationRef ref : clientModule.getApplicationClient().getMessageDestinationRef()) {
            final String destinationId = resolveDestinationId(ref, moduleUri, destinationResolver, destinationTypes);
            if (destinationId != null) {
                // for client modules we put the destinationId in the mapped name
                ref.setMappedName(destinationId);
            }
        }
    }
    for (final WebModule webModule : appModule.getWebModules()) {
        final URI moduleUri = URLs.uri(webModule.getModuleId());
        for (final MessageDestinationRef ref : webModule.getWebApp().getMessageDestinationRef()) {
            final String destinationId = resolveDestinationId(ref, moduleUri, destinationResolver, destinationTypes);
            if (destinationId != null) {
                // for web modules we put the destinationId in the mapped name
                ref.setMappedName(destinationId);
            }
        }
    }
    // the info from the destination (which got filled in from the references)
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
        if (assembly == null) {
            continue;
        }
        final URI moduleUri = URLs.uri(ejbModule.getModuleId());
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
            // MDB destination is deploymentId if none set
            if (bean instanceof MessageDrivenBean) {
                final MessageDrivenBean mdb = (MessageDrivenBean) bean;
                if (!isJms(mdb)) {
                    continue;
                }
                final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
                if (ejbDeployment == null) {
                    throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
                }
                // if destination type is already set in, continue
                String destinationType = mdb.getMessageDestinationType();
                if (destinationType != null) {
                    continue;
                }
                final String link = mdb.getMessageDestinationLink();
                if (link != null && link.length() != 0) {
                    // resolve the destination... if we don't find one it is a configuration bug
                    final MessageDestination destination = destinationResolver.resolveLink(link, moduleUri);
                    if (destination == null) {
                        throw new OpenEJBException("Message destination " + link + " for message driven bean " + mdb.getEjbName() + " not found");
                    }
                    destinationType = destinationTypes.get(destination);
                }
                if (destinationType == null) {
                    // couldn't determine type... we'll have to guess
                    // if destination name contains the string "queue" or "topic" we use that
                    final Properties properties = mdb.getActivationConfig().toProperties();
                    final String destination = properties.getProperty("destination").toLowerCase();
                    if (destination.contains("queue")) {
                        destinationType = Queue.class.getName();
                    } else if (destination.contains("topic")) {
                        destinationType = Topic.class.getName();
                    } else {
                        // Queue is the default
                        destinationType = Queue.class.getName();
                    }
                    logger.info("Auto-configuring a message driven bean " + ejbDeployment.getDeploymentId() + " destination " + properties.getProperty("destination") + " to be destinationType " + destinationType);
                }
                if (destinationType != null) {
                    mdb.getActivationConfig().addProperty("destinationType", destinationType);
                    mdb.setMessageDestinationType(destinationType);
                    // topics need a clientId and subscriptionName
                    if ("javax.jms.Topic".equals(destinationType)) {
                        final Properties properties = mdb.getActivationConfig().toProperties();
                        if (!properties.containsKey("clientId")) {
                            mdb.getActivationConfig().addProperty("clientId", ejbDeployment.getDeploymentId());
                        }
                        if (!properties.containsKey("subscriptionName")) {
                            mdb.getActivationConfig().addProperty("subscriptionName", ejbDeployment.getDeploymentId() + "_subscription");
                        }
                    }
                }
            }
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) MessageDestination(org.apache.openejb.jee.MessageDestination) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) HashMap(java.util.HashMap) Properties(java.util.Properties) SuperProperties(org.apache.openejb.util.SuperProperties) URI(java.net.URI) MessageDestinationRef(org.apache.openejb.jee.MessageDestinationRef) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) LinkResolver(org.apache.openejb.util.LinkResolver) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) ResourceLink(org.apache.openejb.jee.oejb3.ResourceLink) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) AssemblyDescriptor(org.apache.openejb.jee.AssemblyDescriptor) Queue(javax.jms.Queue)

Aggregations

Queue (javax.jms.Queue)47 Session (javax.jms.Session)26 Test (org.junit.Test)25 TextMessage (javax.jms.TextMessage)22 MessageProducer (javax.jms.MessageProducer)21 Message (javax.jms.Message)20 QueueSession (javax.jms.QueueSession)15 Connection (javax.jms.Connection)12 JMSException (javax.jms.JMSException)9 ConnectionFactory (javax.jms.ConnectionFactory)8 MessageConsumer (javax.jms.MessageConsumer)8 ObjectMessage (javax.jms.ObjectMessage)8 Destination (javax.jms.Destination)7 QueueConnection (javax.jms.QueueConnection)7 InitialContext (javax.naming.InitialContext)7 QueueConnectionFactory (javax.jms.QueueConnectionFactory)6 TemporaryQueue (javax.jms.TemporaryQueue)6 BytesMessage (javax.jms.BytesMessage)4 QueueReceiver (javax.jms.QueueReceiver)4 Enumeration (java.util.Enumeration)3