Search in sources :

Example 1 with MessageEndpointFactory

use of javax.resource.spi.endpoint.MessageEndpointFactory in project tomee by apache.

the class MdbTest method createListener.

private void createListener() throws Exception {
    // create the activation spec
    final ActiveMQActivationSpec activationSpec = new ActiveMQActivationSpec();
    activationSpec.setDestinationType("javax.jms.Queue");
    activationSpec.setDestination(REQUEST_QUEUE_NAME);
    // validate the activation spec
    activationSpec.validate();
    // set the resource adapter into the activation spec
    activationSpec.setResourceAdapter(ra);
    // create the message endpoint
    final MessageEndpointFactory endpointFactory = new JmsEndpointFactory();
    // activate the endpoint
    ra.endpointActivation(endpointFactory, activationSpec);
}
Also used : MessageEndpointFactory(javax.resource.spi.endpoint.MessageEndpointFactory) ActiveMQActivationSpec(org.apache.activemq.ra.ActiveMQActivationSpec)

Example 2 with MessageEndpointFactory

use of javax.resource.spi.endpoint.MessageEndpointFactory in project activemq-artemis by apache.

the class ActiveMQMessageHandler method setup.

public void setup() throws Exception {
    if (logger.isTraceEnabled()) {
        logger.trace("setup()");
    }
    ActiveMQActivationSpec spec = activation.getActivationSpec();
    String selector = spec.getMessageSelector();
    // Create the message consumer
    SimpleString selectorString = selector == null || selector.trim().equals("") ? null : new SimpleString(selector);
    if (activation.isTopic() && spec.isSubscriptionDurable()) {
        SimpleString queueName = ActiveMQDestination.createQueueNameForSubscription(true, spec.getClientID(), spec.getSubscriptionName());
        QueueQuery subResponse = session.queueQuery(queueName);
        if (!subResponse.isExists()) {
            session.createQueue(activation.getAddress(), queueName, selectorString, true);
        } else {
            // As a deployed MDB could set up multiple instances in order to process messages in parallel.
            if (sessionNr == 0 && subResponse.getConsumerCount() > 0) {
                if (!spec.isShareSubscriptions()) {
                    throw ActiveMQRALogger.LOGGER.canNotCreatedNonSharedSubscriber();
                } else if (ActiveMQRALogger.LOGGER.isDebugEnabled()) {
                    logger.debug("the mdb on destination " + queueName + " already had " + subResponse.getConsumerCount() + " consumers but the MDB is configured to share subscriptions, so no exceptions are thrown");
                }
            }
            SimpleString oldFilterString = subResponse.getFilterString();
            boolean selectorChanged = selector == null && oldFilterString != null || oldFilterString == null && selector != null || (oldFilterString != null && selector != null && !oldFilterString.toString().equals(selector));
            SimpleString oldTopicName = subResponse.getAddress();
            boolean topicChanged = !oldTopicName.equals(activation.getAddress());
            if (selectorChanged || topicChanged) {
                // Delete the old durable sub
                session.deleteQueue(queueName);
                // Create the new one
                session.createQueue(activation.getAddress(), queueName, selectorString, true);
            }
        }
        consumer = (ClientConsumerInternal) session.createConsumer(queueName, null, false);
    } else {
        SimpleString tempQueueName;
        if (activation.isTopic()) {
            if (activation.getTopicTemporaryQueue() == null) {
                tempQueueName = new SimpleString(UUID.randomUUID().toString());
                session.createTemporaryQueue(activation.getAddress(), tempQueueName, selectorString);
                activation.setTopicTemporaryQueue(tempQueueName);
            } else {
                tempQueueName = activation.getTopicTemporaryQueue();
                QueueQuery queueQuery = session.queueQuery(tempQueueName);
                if (!queueQuery.isExists()) {
                    // this is because we could be using remote servers (in cluster maybe)
                    // and the queue wasn't created on that node yet.
                    session.createTemporaryQueue(activation.getAddress(), tempQueueName, selectorString);
                }
            }
        } else {
            tempQueueName = activation.getAddress();
        }
        consumer = (ClientConsumerInternal) session.createConsumer(tempQueueName, selectorString);
    }
    // Create the endpoint, if we are transacted pass the session so it is enlisted, unless using Local TX
    MessageEndpointFactory endpointFactory = activation.getMessageEndpointFactory();
    useLocalTx = !activation.isDeliveryTransacted() && activation.getActivationSpec().isUseLocalTx();
    transacted = activation.isDeliveryTransacted();
    if (activation.isDeliveryTransacted() && !activation.getActivationSpec().isUseLocalTx()) {
        Map<String, Object> xaResourceProperties = new HashMap<>();
        xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_JNDI_NAME, ((ActiveMQResourceAdapter) spec.getResourceAdapter()).getJndiName());
        xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_NODE_ID, ((ClientSessionFactoryInternal) cf).getLiveNodeId());
        xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_PRODUCT_NAME, ActiveMQResourceAdapter.PRODUCT_NAME);
        xaResourceProperties.put(ActiveMQXAResourceWrapper.ACTIVEMQ_PRODUCT_VERSION, VersionLoader.getVersion().getFullVersion());
        XAResource xaResource = ServiceUtils.wrapXAResource(session, xaResourceProperties);
        endpoint = endpointFactory.createEndpoint(xaResource);
        useXA = true;
    } else {
        endpoint = endpointFactory.createEndpoint(null);
        useXA = false;
    }
    connected = true;
    session.addFailoverListener(this);
    consumer.setMessageHandler(this);
}
Also used : MessageEndpointFactory(javax.resource.spi.endpoint.MessageEndpointFactory) XAResource(javax.transaction.xa.XAResource) HashMap(java.util.HashMap) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) QueueQuery(org.apache.activemq.artemis.api.core.client.ClientSession.QueueQuery)

Aggregations

MessageEndpointFactory (javax.resource.spi.endpoint.MessageEndpointFactory)2 HashMap (java.util.HashMap)1 XAResource (javax.transaction.xa.XAResource)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 QueueQuery (org.apache.activemq.artemis.api.core.client.ClientSession.QueueQuery)1 ActiveMQActivationSpec (org.apache.activemq.ra.ActiveMQActivationSpec)1