Search in sources :

Example 1 with ClientSessionImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionImpl in project activemq-artemis by apache.

the class JmsProducerTest method multipleSendsUsingSetters.

@Test
public void multipleSendsUsingSetters() throws Exception {
    server.createQueue(SimpleString.toSimpleString("q1"), RoutingType.ANYCAST, SimpleString.toSimpleString("q1"), null, true, false);
    Queue q1 = context.createQueue("q1");
    context.createProducer().setProperty("prop1", 1).setProperty("prop2", 2).send(q1, "Text1");
    context.createProducer().setProperty("prop1", 3).setProperty("prop2", 4).send(q1, "Text2");
    for (int i = 0; i < 100; i++) {
        context.createProducer().send(q1, "Text" + i);
    }
    ActiveMQSession sessionUsed = (ActiveMQSession) (((ActiveMQJMSContext) context).getUsedSession());
    ClientSessionImpl coreSession = (ClientSessionImpl) sessionUsed.getCoreSession();
    // JMSConsumer is supposed to cache the producer, each call to createProducer is supposed to always return the same producer
    assertEquals(1, coreSession.cloneProducers().size());
    JMSConsumer consumer = context.createConsumer(q1);
    TextMessage text = (TextMessage) consumer.receive(5000);
    assertNotNull(text);
    assertEquals("Text1", text.getText());
    assertEquals(1, text.getIntProperty("prop1"));
    assertEquals(2, text.getIntProperty("prop2"));
    text = (TextMessage) consumer.receive(5000);
    assertNotNull(text);
    assertEquals("Text2", text.getText());
    assertEquals(3, text.getIntProperty("prop1"));
    assertEquals(4, text.getIntProperty("prop2"));
    for (int i = 0; i < 100; i++) {
        assertEquals("Text" + i, consumer.receiveBody(String.class, 1000));
    }
    consumer.close();
    context.close();
}
Also used : ActiveMQJMSContext(org.apache.activemq.artemis.jms.client.ActiveMQJMSContext) JMSConsumer(javax.jms.JMSConsumer) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionImpl) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 2 with ClientSessionImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionImpl in project activemq-artemis by apache.

the class ActiveMQServerControlTest method testCloseJMSclient.

@Test
public void testCloseJMSclient() throws Exception {
    ActiveMQServerControl serverControl = createManagementControl();
    ConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    Connection conn = cf.createConnection();
    conn.start();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    javax.jms.Topic topic = ActiveMQJMSClient.createTopic("ConsumerTestTopic");
    MessageConsumer JMSclient = session.createConsumer(topic, "test1");
    long clientID = -1;
    String sessionID = ((ClientSessionImpl) (((ActiveMQSession) session).getCoreSession())).getName();
    Set<ServerSession> sessions = server.getSessions();
    for (ServerSession sess : sessions) {
        if (sess.getName().equals(sessionID.toString())) {
            Set<ServerConsumer> serverConsumers = sess.getServerConsumers();
            for (ServerConsumer serverConsumer : serverConsumers) {
                clientID = serverConsumer.sequentialID();
            }
        }
    }
    Assert.assertFalse(((org.apache.activemq.artemis.jms.client.ActiveMQMessageConsumer) JMSclient).isClosed());
    serverControl.closeConsumerWithID(sessionID, Long.toString(clientID));
    Wait.waitFor(() -> ((org.apache.activemq.artemis.jms.client.ActiveMQMessageConsumer) JMSclient).isClosed());
    Assert.assertTrue(((org.apache.activemq.artemis.jms.client.ActiveMQMessageConsumer) JMSclient).isClosed());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) Connection(javax.jms.Connection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ServerConsumer(org.apache.activemq.artemis.core.server.ServerConsumer) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSessionImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionImpl) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 3 with ClientSessionImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionImpl in project activemq-artemis by apache.

the class ActiveMQServerControlTest method lookupSession.

private JsonObject lookupSession(JsonArray jsonArray, ClientSession session) throws Exception {
    String name = ((ClientSessionImpl) session).getName();
    for (int i = 0; i < jsonArray.size(); i++) {
        JsonObject obj = jsonArray.getJsonObject(i);
        String sessionID = obj.getString("sessionID");
        Assert.assertNotNull(sessionID);
        if (sessionID.equals(name)) {
            return obj;
        }
    }
    Assert.fail("Sesison not found for session id " + name);
    // not going to happen, fail will throw an exception but it won't compile without this
    return null;
}
Also used : JsonObject(javax.json.JsonObject) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionImpl)

Example 4 with ClientSessionImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionImpl in project activemq-artemis by apache.

the class ActiveMQServerControlTest method testListSessions.

@Test
public void testListSessions() throws Exception {
    SimpleString queueName1 = new SimpleString("my_queue_one");
    SimpleString addressName1 = new SimpleString("my_address_one");
    ActiveMQServerControl serverControl = createManagementControl();
    server.addAddressInfo(new AddressInfo(addressName1, RoutingType.ANYCAST));
    server.createQueue(addressName1, RoutingType.ANYCAST, queueName1, null, false, false);
    // create some consumers
    try (ServerLocator locator = createInVMNonHALocator();
        ClientSessionFactory csf = createSessionFactory(locator)) {
        ClientSessionImpl session1 = (ClientSessionImpl) csf.createSession();
        Thread.sleep(500);
        ClientSessionImpl session2 = (ClientSessionImpl) csf.createSession();
        Thread.sleep(500);
        ClientSessionImpl session3 = (ClientSessionImpl) csf.createSession();
        ClientConsumer consumer1_s1 = session1.createConsumer(queueName1);
        ClientConsumer consumer2_s1 = session1.createConsumer(queueName1);
        ClientConsumer consumer1_s2 = session2.createConsumer(queueName1);
        ClientConsumer consumer2_s2 = session2.createConsumer(queueName1);
        ClientConsumer consumer3_s2 = session2.createConsumer(queueName1);
        ClientConsumer consumer4_s2 = session2.createConsumer(queueName1);
        ClientConsumer consumer1_s3 = session3.createConsumer(queueName1);
        ClientConsumer consumer2_s3 = session3.createConsumer(queueName1);
        ClientConsumer consumer3_s3 = session3.createConsumer(queueName1);
        String filterString = createJsonFilter("CONSUMER_COUNT", "GREATER_THAN", "1");
        String sessionsAsJsonString = serverControl.listSessions(filterString, 1, 50);
        JsonObject sessionsAsJsonObject = JsonUtil.readJsonObject(sessionsAsJsonString);
        JsonArray array = (JsonArray) sessionsAsJsonObject.get("data");
        Assert.assertEquals("number of sessions returned from query", 3, array.size());
        JsonObject jsonSession = array.getJsonObject(0);
        // check all fields
        Assert.assertNotEquals("id", "", jsonSession.getString("id"));
        Assert.assertEquals("user", "", jsonSession.getString("user"));
        Assert.assertNotEquals("creationTime", "", jsonSession.getString("creationTime"));
        Assert.assertEquals("consumerCount", 2, jsonSession.getInt("consumerCount"));
        Assert.assertTrue("producerCount", 0 <= jsonSession.getInt("producerCount"));
        Assert.assertNotEquals("connectionID", "", jsonSession.getString("connectionID"));
        // check default order
        Assert.assertEquals("session1 location", session1.getName(), array.getJsonObject(0).getString("id"));
        Assert.assertEquals("session2 location", session2.getName(), array.getJsonObject(1).getString("id"));
        Assert.assertEquals("session3 location", session3.getName(), array.getJsonObject(2).getString("id"));
        // bring back session ordered by consumer count
        filterString = createJsonFilter("CONSUMER_COUNT", "GREATER_THAN", "1", "consumerCount", "asc");
        sessionsAsJsonString = serverControl.listSessions(filterString, 1, 50);
        sessionsAsJsonObject = JsonUtil.readJsonObject(sessionsAsJsonString);
        array = (JsonArray) sessionsAsJsonObject.get("data");
        Assert.assertTrue("number of sessions returned from query", 3 == array.size());
        Assert.assertEquals("session1 ordered by consumer", session1.getName(), array.getJsonObject(0).getString("id"));
        Assert.assertEquals("session3 ordered by consumer", session3.getName(), array.getJsonObject(1).getString("id"));
        Assert.assertEquals("session2 ordered by consumer", session2.getName(), array.getJsonObject(2).getString("id"));
        // bring back session ordered by consumer Count
        filterString = createJsonFilter("CONSUMER_COUNT", "GREATER_THAN", "1", "consumerCount", "asc");
        sessionsAsJsonString = serverControl.listSessions(filterString, 1, 50);
        sessionsAsJsonObject = JsonUtil.readJsonObject(sessionsAsJsonString);
        array = (JsonArray) sessionsAsJsonObject.get("data");
        Assert.assertTrue("number of sessions returned from query", 3 == array.size());
        Assert.assertEquals("session1 ordered by consumer", session1.getName(), array.getJsonObject(0).getString("id"));
        Assert.assertEquals("session3 ordered by consumer", session3.getName(), array.getJsonObject(1).getString("id"));
        Assert.assertEquals("session2 ordered by consumer", session2.getName(), array.getJsonObject(2).getString("id"));
        // bring back session ordered by creation time (desc)
        filterString = createJsonFilter("CONSUMER_COUNT", "GREATER_THAN", "1", "creationTime", "desc");
        sessionsAsJsonString = serverControl.listSessions(filterString, 1, 50);
        sessionsAsJsonObject = JsonUtil.readJsonObject(sessionsAsJsonString);
        array = (JsonArray) sessionsAsJsonObject.get("data");
        Assert.assertTrue("number of sessions returned from query", 3 == array.size());
        Assert.assertEquals("session3 ordered by creationTime", session3.getName(), array.getJsonObject(0).getString("id"));
        Assert.assertEquals("session2 ordered by creationTime", session2.getName(), array.getJsonObject(1).getString("id"));
        Assert.assertEquals("session1 ordered by creationTime", session1.getName(), array.getJsonObject(2).getString("id"));
    }
}
Also used : JsonArray(javax.json.JsonArray) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JsonObject(javax.json.JsonObject) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionImpl) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) Test(org.junit.Test)

Example 5 with ClientSessionImpl

use of org.apache.activemq.artemis.core.client.impl.ClientSessionImpl in project activemq-artemis by apache.

the class ActiveMQServerControlTest method testListConsumersOrder.

@Test
public void testListConsumersOrder() throws Exception {
    SimpleString queueName1 = new SimpleString("my_queue_one");
    SimpleString addressName1 = new SimpleString("my_address_one");
    ActiveMQServerControl serverControl = createManagementControl();
    server.addAddressInfo(new AddressInfo(addressName1, RoutingType.ANYCAST));
    server.createQueue(addressName1, RoutingType.ANYCAST, queueName1, null, false, false);
    try (ServerLocator locator = createInVMNonHALocator();
        ClientSessionFactory csf = createSessionFactory(locator)) {
        ClientSessionImpl session1 = (ClientSessionImpl) csf.createSession();
        ClientSessionImpl session2 = (ClientSessionImpl) csf.createSession();
        ClientSessionImpl session3 = (ClientSessionImpl) csf.createSession();
        // sleep - test compares creationTimes
        ClientConsumer consumer_s1 = session1.createConsumer(queueName1);
        Thread.sleep(500);
        ClientConsumer consumer_s2 = session2.createConsumer(queueName1);
        Thread.sleep(500);
        ClientConsumer consumer_s3 = session3.createConsumer(queueName1);
        // test default Order
        String filterString = createJsonFilter("queue", "EQUALS", queueName1.toString());
        String consumersAsJsonString = serverControl.listConsumers(filterString, 1, 50);
        JsonObject consumersAsJsonObject = JsonUtil.readJsonObject(consumersAsJsonString);
        JsonArray array = (JsonArray) consumersAsJsonObject.get("data");
        Assert.assertEquals("number of consumers returned from query", 3, array.size());
        Assert.assertEquals("Consumer1 default order", session1.getName(), array.getJsonObject(0).getString("session"));
        Assert.assertEquals("Consumer2 default order", session2.getName(), array.getJsonObject(1).getString("session"));
        Assert.assertEquals("Consumer3 default order", session3.getName(), array.getJsonObject(2).getString("session"));
        // test ordered by creationTime
        filterString = createJsonFilter("queue", "EQUALS", queueName1.toString(), "creationTime", "desc");
        consumersAsJsonString = serverControl.listConsumers(filterString, 1, 50);
        consumersAsJsonObject = JsonUtil.readJsonObject(consumersAsJsonString);
        array = (JsonArray) consumersAsJsonObject.get("data");
        Assert.assertEquals("number of consumers returned from query", 3, array.size());
        Assert.assertEquals("Consumer3 creation time", session3.getName(), array.getJsonObject(0).getString("session"));
        Assert.assertEquals("Consumer2 creation time", session2.getName(), array.getJsonObject(1).getString("session"));
        Assert.assertEquals("Consumer1 creation time", session1.getName(), array.getJsonObject(2).getString("session"));
    }
}
Also used : JsonArray(javax.json.JsonArray) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JsonObject(javax.json.JsonObject) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionImpl) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) Test(org.junit.Test)

Aggregations

SimpleString (org.apache.activemq.artemis.api.core.SimpleString)5 ClientSessionImpl (org.apache.activemq.artemis.core.client.impl.ClientSessionImpl)5 Test (org.junit.Test)4 JsonObject (javax.json.JsonObject)3 ActiveMQServerControl (org.apache.activemq.artemis.api.core.management.ActiveMQServerControl)3 JsonArray (javax.json.JsonArray)2 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)2 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)2 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)2 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)2 ActiveMQSession (org.apache.activemq.artemis.jms.client.ActiveMQSession)2 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 JMSConsumer (javax.jms.JMSConsumer)1 MessageConsumer (javax.jms.MessageConsumer)1 Queue (javax.jms.Queue)1 Session (javax.jms.Session)1 TextMessage (javax.jms.TextMessage)1 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)1 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)1