Search in sources :

Example 81 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class NonExistentQueueTest method sendToNonExistentDestination.

@Test
public void sendToNonExistentDestination() throws Exception {
    server.getAddressSettingsRepository().addMatch("#", new AddressSettings().setAutoCreateQueues(false));
    server.getAddressSettingsRepository().addMatch("#", new AddressSettings().setAutoCreateAddresses(false));
    Destination destination = ActiveMQJMSClient.createTopic("DoesNotExist");
    TransportConfiguration transportConfiguration = new TransportConfiguration(InVMConnectorFactory.class.getName());
    ConnectionFactory localConnectionFactory = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, transportConfiguration);
    // Using JMS 1 API
    Connection connection = localConnectionFactory.createConnection();
    Session session = connection.createSession();
    try {
        MessageProducer messageProducer = session.createProducer(null);
        messageProducer.send(destination, session.createMessage());
        Assert.fail("Succeeded in sending message to a non-existent destination using JMS 1 API!");
    } catch (JMSException e) {
    // Expected }
    }
    // Using JMS 2 API
    JMSContext context = localConnectionFactory.createContext();
    JMSProducer jmsProducer = context.createProducer().setDeliveryMode(DeliveryMode.PERSISTENT);
    try {
        jmsProducer.send(destination, context.createMessage());
        Assert.fail("Succeeded in sending message to a non-existent destination using JMS 2 API!");
    } catch (JMSRuntimeException e) {
    // Expected }
    }
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory) JMSException(javax.jms.JMSException) JMSProducer(javax.jms.JMSProducer) MessageProducer(javax.jms.MessageProducer) JMSContext(javax.jms.JMSContext) Session(javax.jms.Session) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 82 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class JMSSecurityTest method testSecurityOnJMSContext.

@Test
public void testSecurityOnJMSContext() throws Exception {
    ActiveMQJAASSecurityManager securityManager = (ActiveMQJAASSecurityManager) server.getSecurityManager();
    securityManager.getConfiguration().addUser("IDo", "Exist");
    try {
        JMSContext ctx = cf.createContext("Idont", "exist");
        ctx.close();
    } catch (JMSSecurityRuntimeException e) {
    // expected
    }
    JMSContext ctx = cf.createContext("IDo", "Exist");
    ctx.close();
}
Also used : JMSSecurityRuntimeException(javax.jms.JMSSecurityRuntimeException) ActiveMQJAASSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 83 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class JMSSecurityTest method testCreateQueueConnection.

@Test
public void testCreateQueueConnection() throws Exception {
    ActiveMQJAASSecurityManager securityManager = (ActiveMQJAASSecurityManager) server.getSecurityManager();
    securityManager.getConfiguration().addUser("IDo", "Exist");
    try {
        QueueConnection queueC = ((QueueConnectionFactory) cf).createQueueConnection("IDont", "Exist");
        fail("supposed to throw exception");
        queueC.close();
    } catch (JMSSecurityException e) {
    // expected
    }
    JMSContext ctx = cf.createContext("IDo", "Exist");
    ctx.close();
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueConnectionFactory(javax.jms.QueueConnectionFactory) JMSSecurityException(javax.jms.JMSSecurityException) ActiveMQJAASSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 84 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class ConnectionTest method testThroughNewConnectionFactory.

private void testThroughNewConnectionFactory(ActiveMQConnectionFactory factory) throws Exception {
    Connection conn = factory.createConnection();
    conn.close();
    try (JMSContext ctx = factory.createContext()) {
        ctx.createProducer().send(ctx.createQueue("queue"), "Test");
    }
    try (JMSContext ctx = factory.createContext()) {
        Assert.assertNotNull(ctx.createConsumer(ctx.createQueue("queue")).receiveNoWait());
        Assert.assertNull(ctx.createConsumer(ctx.createQueue("queue")).receiveNoWait());
    }
    factory.close();
}
Also used : Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) XAConnection(javax.jms.XAConnection) QueueConnection(javax.jms.QueueConnection) JMSContext(javax.jms.JMSContext)

Example 85 with JMSContext

use of javax.jms.JMSContext in project activemq-artemis by apache.

the class GroupingTest method testGroupingWithJMS2Producer.

@Test
public void testGroupingWithJMS2Producer() throws Exception {
    ConnectionFactory fact = getCF();
    Assume.assumeFalse("only makes sense withOUT auto-group", ((ActiveMQConnectionFactory) fact).isAutoGroup());
    Assume.assumeTrue("only makes sense withOUT explicit group-id", ((ActiveMQConnectionFactory) fact).getGroupID() == null);
    final String groupID = UUID.randomUUID().toString();
    JMSContext ctx = addContext(getCF().createContext(JMSContext.SESSION_TRANSACTED));
    JMSProducer producer = ctx.createProducer().setProperty("JMSXGroupID", groupID);
    JMSConsumer consumer1 = ctx.createConsumer(queue);
    JMSConsumer consumer2 = ctx.createConsumer(queue);
    JMSConsumer consumer3 = ctx.createConsumer(queue);
    ctx.start();
    for (int j = 0; j < 100; j++) {
        TextMessage message = ctx.createTextMessage("Message" + j);
        producer.send(queue, message);
        String prop = message.getStringProperty("JMSXGroupID");
        assertNotNull(prop);
        assertEquals(groupID, prop);
    }
    ctx.commit();
    // All msgs should go to the first consumer
    for (int j = 0; j < 100; j++) {
        TextMessage tm = (TextMessage) consumer1.receive(10000);
        assertNotNull(tm);
        tm.acknowledge();
        assertEquals("Message" + j, tm.getText());
        assertEquals(tm.getStringProperty("JMSXGroupID"), groupID);
        tm = (TextMessage) consumer2.receiveNoWait();
        assertNull(tm);
        tm = (TextMessage) consumer3.receiveNoWait();
        assertNull(tm);
    }
    ctx.commit();
    ctx.close();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) JMSConsumer(javax.jms.JMSConsumer) JMSProducer(javax.jms.JMSProducer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSContext(javax.jms.JMSContext) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.artemis.jms.client.ActiveMQTextMessage) Test(org.junit.Test)

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