Search in sources :

Example 16 with ActiveMQDestination

use of org.apache.activemq.artemis.jms.client.ActiveMQDestination in project activemq-artemis by apache.

the class EmbeddedJMSResource method getDestinationQueue.

/**
 * Get the Queue corresponding to a JMS Destination.
 * <p>
 * The full name of the JMS destination including the prefix should be provided - i.e. queue://myQueue
 * or topic://myTopic.  If the destination type prefix is not included in the destination name, a prefix
 * of "queue://" is assumed.
 *
 * @param destinationName the full name of the JMS Destination
 * @return the number of messages in the JMS Destination
 */
public Queue getDestinationQueue(String destinationName) {
    Queue queue = null;
    ActiveMQDestination destination = ActiveMQDestination.createDestination(destinationName, ActiveMQDestination.TYPE.QUEUE);
    String address = destination.getAddress();
    String name = destination.getName();
    if (destination.isQueue()) {
        queue = jmsServer.getActiveMQServer().locateQueue(destination.getSimpleAddress());
    } else {
        BindingQueryResult bindingQueryResult = null;
        try {
            bindingQueryResult = jmsServer.getActiveMQServer().bindingQuery(destination.getSimpleAddress());
        } catch (Exception ex) {
            log.error(String.format("getDestinationQueue( %s ) - bindingQuery for %s failed", destinationName, destination.getAddress()), ex);
            return null;
        }
        if (bindingQueryResult.isExists()) {
            List<SimpleString> queueNames = bindingQueryResult.getQueueNames();
            if (queueNames.size() > 0) {
                queue = jmsServer.getActiveMQServer().locateQueue(queueNames.get(0));
            }
        }
    }
    return queue;
}
Also used : BindingQueryResult(org.apache.activemq.artemis.core.server.BindingQueryResult) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Queue(org.apache.activemq.artemis.core.server.Queue) JMSException(javax.jms.JMSException) ActiveMQDestination(org.apache.activemq.artemis.jms.client.ActiveMQDestination)

Example 17 with ActiveMQDestination

use of org.apache.activemq.artemis.jms.client.ActiveMQDestination in project activemq-artemis by apache.

the class JMSTest method createDestination.

public static Destination createDestination(String dest) {
    ActiveMQDestination destination = (ActiveMQDestination) ActiveMQDestination.fromPrefixedName(dest);
    System.out.println("SimpleAddress: " + destination.getSimpleAddress());
    return destination;
}
Also used : ActiveMQDestination(org.apache.activemq.artemis.jms.client.ActiveMQDestination)

Example 18 with ActiveMQDestination

use of org.apache.activemq.artemis.jms.client.ActiveMQDestination in project activemq-artemis by apache.

the class SelectorTest method createDestination.

public static Destination createDestination(String dest) {
    ActiveMQDestination destination = (ActiveMQDestination) ActiveMQDestination.fromPrefixedName(dest);
    System.out.println("SimpleAddress: " + destination.getSimpleAddress());
    return destination;
}
Also used : ActiveMQDestination(org.apache.activemq.artemis.jms.client.ActiveMQDestination)

Example 19 with ActiveMQDestination

use of org.apache.activemq.artemis.jms.client.ActiveMQDestination in project activemq-artemis by apache.

the class ActiveMQActivation method setupDestination.

protected void setupDestination() throws Exception {
    String destinationName = spec.getDestination();
    if (spec.isUseJNDI()) {
        Context ctx;
        if (spec.getParsedJndiParams() == null) {
            ctx = new InitialContext();
        } else {
            ctx = new InitialContext(spec.getParsedJndiParams());
        }
        logger.debug("Using context " + ctx.getEnvironment() + " for " + spec);
        if (logger.isTraceEnabled()) {
            logger.trace("setupDestination(" + ctx + ")");
        }
        String destinationTypeString = spec.getDestinationType();
        if (destinationTypeString != null && !destinationTypeString.trim().equals("")) {
            logger.debug("Destination type defined as " + destinationTypeString);
            Class<?> destinationType;
            if (Topic.class.getName().equals(destinationTypeString)) {
                destinationType = Topic.class;
                isTopic = true;
            } else {
                destinationType = Queue.class;
            }
            logger.debug("Retrieving " + destinationType.getName() + " \"" + destinationName + "\" from JNDI");
            try {
                destination = (ActiveMQDestination) ActiveMQRaUtils.lookup(ctx, destinationName, destinationType);
            } catch (Exception e) {
                if (destinationName == null) {
                    throw ActiveMQRABundle.BUNDLE.noDestinationName();
                }
                String calculatedDestinationName = destinationName.substring(destinationName.lastIndexOf('/') + 1);
                if (isTopic && spec.getTopicPrefix() != null) {
                    calculatedDestinationName = spec.getTopicPrefix() + calculatedDestinationName;
                } else if (!isTopic && spec.getQueuePrefix() != null) {
                    calculatedDestinationName = spec.getQueuePrefix() + calculatedDestinationName;
                }
                logger.debug("Unable to retrieve " + destinationName + " from JNDI. Creating a new " + destinationType.getName() + " named " + calculatedDestinationName + " to be used by the MDB.");
                // If there is no binding on naming, we will just create a new instance
                if (isTopic) {
                    destination = (ActiveMQDestination) ActiveMQJMSClient.createTopic(calculatedDestinationName);
                } else {
                    destination = (ActiveMQDestination) ActiveMQJMSClient.createQueue(calculatedDestinationName);
                }
            }
        } else {
            logger.debug("Destination type not defined in MDB activation configuration.");
            logger.debug("Retrieving " + Destination.class.getName() + " \"" + destinationName + "\" from JNDI");
            destination = (ActiveMQDestination) ActiveMQRaUtils.lookup(ctx, destinationName, Destination.class);
            if (destination instanceof Topic) {
                isTopic = true;
            }
        }
    } else {
        ActiveMQRALogger.LOGGER.instantiatingDestination(spec.getDestinationType(), spec.getDestination());
        if (Topic.class.getName().equals(spec.getDestinationType())) {
            destination = (ActiveMQDestination) ActiveMQJMSClient.createTopic(spec.getDestination());
            isTopic = true;
        } else {
            destination = (ActiveMQDestination) ActiveMQJMSClient.createQueue(spec.getDestination());
        }
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) ActiveMQDestination(org.apache.activemq.artemis.jms.client.ActiveMQDestination) Destination(javax.jms.Destination) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Topic(javax.jms.Topic) InitialContext(javax.naming.InitialContext) ResourceException(javax.resource.ResourceException) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQNonExistentQueueException(org.apache.activemq.artemis.api.core.ActiveMQNonExistentQueueException) ActiveMQDestination(org.apache.activemq.artemis.jms.client.ActiveMQDestination)

Example 20 with ActiveMQDestination

use of org.apache.activemq.artemis.jms.client.ActiveMQDestination in project activemq-artemis by apache.

the class LVQTest method testLastValueQueueUsingAddressQueueParameters.

@Test
public void testLastValueQueueUsingAddressQueueParameters() throws Exception {
    ActiveMQConnectionFactory fact = (ActiveMQConnectionFactory) getCF();
    // Set the consumer window size to 0 to not buffer any messages client side.
    fact.setConsumerWindowSize(0);
    Connection connection = fact.createConnection();
    try {
        Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        Queue queue = session.createQueue("random?last-value=true");
        assertEquals("random", queue.getQueueName());
        ActiveMQDestination a = (ActiveMQDestination) queue;
        assertTrue(a.getQueueAttributes().getLastValue());
        MessageProducer producer = session.createProducer(queue);
        MessageConsumer consumer1 = session.createConsumer(queue);
        connection.start();
        for (int j = 0; j < 100; j++) {
            TextMessage message = session.createTextMessage();
            message.setText("Message" + j);
            message.setStringProperty(Message.HDR_LAST_VALUE_NAME.toString(), "key");
            producer.send(message);
        }
        // Last message only should go to the consumer
        TextMessage tm = (TextMessage) consumer1.receive(10000);
        assertNotNull(tm);
        assertEquals("Message99", tm.getText());
    } finally {
        connection.close();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQDestination(org.apache.activemq.artemis.jms.client.ActiveMQDestination) Test(org.junit.Test)

Aggregations

ActiveMQDestination (org.apache.activemq.artemis.jms.client.ActiveMQDestination)21 Test (org.junit.Test)14 Connection (javax.jms.Connection)6 MessageConsumer (javax.jms.MessageConsumer)4 Session (javax.jms.Session)4 Reference (javax.naming.Reference)4 MessageProducer (javax.jms.MessageProducer)3 Queue (javax.jms.Queue)3 TextMessage (javax.jms.TextMessage)3 Topic (javax.jms.Topic)3 ObjectFactory (javax.naming.spi.ObjectFactory)3 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)3 Queue (org.apache.activemq.artemis.core.server.Queue)3 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)3 Destination (javax.jms.Destination)2 JMSException (javax.jms.JMSException)2 Referenceable (javax.naming.Referenceable)2 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)2 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)2 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)2