Search in sources :

Example 1 with ClientMessageImpl

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

the class BMFailoverTest method testFailoverOnCommit2.

@Test
@BMRules(rules = { @BMRule(name = "trace clientsessionimpl commit", targetClass = "org.apache.activemq.artemis.core.client.impl.ClientSessionImpl", targetMethod = "start(javax.transaction.xa.Xid, int)", targetLocation = "AT EXIT", action = "org.apache.activemq.artemis.tests.extras.byteman.BMFailoverTest.serverToStop.getServer().stop(true)") })
public void testFailoverOnCommit2() throws Exception {
    serverToStop = liveServer;
    locator = getServerLocator().setFailoverOnInitialConnection(true);
    SimpleString inQueue = new SimpleString("inQueue");
    SimpleString outQueue = new SimpleString("outQueue");
    createSessionFactory();
    createSessionFactory2();
    // closeable will take care of closing it
    try (ClientSession session = sf.createSession(false, true, true);
        ClientProducer sendInitialProducer = session.createProducer()) {
        session.createQueue(inQueue, inQueue, null, true);
        session.createQueue(outQueue, outQueue, null, true);
        sendInitialProducer.send(inQueue, createMessage(session, 0, true));
    }
    ClientSession xaSessionRec = addClientSession(sf.createSession(true, false, false));
    ClientConsumer consumer = addClientConsumer(xaSessionRec.createConsumer(inQueue));
    byte[] globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes();
    Xid xidRec = new XidImpl("xa2".getBytes(), 1, globalTransactionId);
    xaSessionRec.start();
    xaSessionRec.getXAResource().start(xidRec, XAResource.TMNOFLAGS);
    // failover is now occurring, receive, ack and end will be called whilst this is happening.
    ClientMessageImpl m = (ClientMessageImpl) consumer.receive(5000);
    assertNotNull(m);
    System.out.println("********************" + m.getIntProperty("counter"));
    // the mdb would ack the message before calling onMessage()
    m.acknowledge();
    try {
        // this may fail but thats ok, it depends on the race and when failover actually happens
        xaSessionRec.end(xidRec, XAResource.TMSUCCESS);
    } catch (XAException ignore) {
    }
    // we always reset the client on the RA
    ((ClientSessionInternal) xaSessionRec).resetIfNeeded();
    // closeable will take care of closing it
    try (ClientSession session = sf.createSession(false, true, true);
        ClientProducer sendInitialProducer = session.createProducer()) {
        sendInitialProducer.send(inQueue, createMessage(session, 0, true));
    }
    // now receive and send a message successfully
    globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes();
    xidRec = new XidImpl("xa4".getBytes(), 1, globalTransactionId);
    xaSessionRec.getXAResource().start(xidRec, XAResource.TMNOFLAGS);
    Binding binding = backupServer.getServer().getPostOffice().getBinding(inQueue);
    Queue inQ = (Queue) binding.getBindable();
    m = (ClientMessageImpl) consumer.receive(5000);
    assertNotNull(m);
    // the mdb would ack the message before calling onMessage()
    m.acknowledge();
    System.out.println("********************" + m.getIntProperty("counter"));
    xaSessionRec.getXAResource().end(xidRec, XAResource.TMSUCCESS);
    xaSessionRec.getXAResource().prepare(xidRec);
    xaSessionRec.getXAResource().commit(xidRec, false);
    // let's close the consumer so anything pending is handled
    consumer.close();
    assertEquals(1, getMessageCount(inQ));
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Queue(org.apache.activemq.artemis.core.server.Queue) ClientMessageImpl(org.apache.activemq.artemis.core.client.impl.ClientMessageImpl) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 2 with ClientMessageImpl

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

the class MessageImplTest method testExpired.

@Test
public void testExpired() {
    Message message = new ClientMessageImpl();
    Assert.assertEquals(0, message.getExpiration());
    Assert.assertFalse(message.isExpired());
    message.setExpiration(System.currentTimeMillis() + 1000);
    Assert.assertFalse(message.isExpired());
    message.setExpiration(System.currentTimeMillis() - 1);
    Assert.assertTrue(message.isExpired());
    message.setExpiration(System.currentTimeMillis() - 1000);
    Assert.assertTrue(message.isExpired());
    message.setExpiration(0);
    Assert.assertFalse(message.isExpired());
}
Also used : CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) SessionSendMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionSendMessage) ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) Message(org.apache.activemq.artemis.api.core.Message) ClientMessageImpl(org.apache.activemq.artemis.core.client.impl.ClientMessageImpl) Test(org.junit.Test)

Example 3 with ClientMessageImpl

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

the class MessageImplTest method testProperties.

@Test
public void testProperties() {
    for (int j = 0; j < 10; j++) {
        Message msg = new ClientMessageImpl();
        SimpleString prop1 = new SimpleString("prop1");
        boolean val1 = RandomUtil.randomBoolean();
        msg.putBooleanProperty(prop1, val1);
        SimpleString prop2 = new SimpleString("prop2");
        byte val2 = RandomUtil.randomByte();
        msg.putByteProperty(prop2, val2);
        SimpleString prop3 = new SimpleString("prop3");
        byte[] val3 = RandomUtil.randomBytes();
        msg.putBytesProperty(prop3, val3);
        SimpleString prop4 = new SimpleString("prop4");
        double val4 = RandomUtil.randomDouble();
        msg.putDoubleProperty(prop4, val4);
        SimpleString prop5 = new SimpleString("prop5");
        float val5 = RandomUtil.randomFloat();
        msg.putFloatProperty(prop5, val5);
        SimpleString prop6 = new SimpleString("prop6");
        int val6 = RandomUtil.randomInt();
        msg.putIntProperty(prop6, val6);
        SimpleString prop7 = new SimpleString("prop7");
        long val7 = RandomUtil.randomLong();
        msg.putLongProperty(prop7, val7);
        SimpleString prop8 = new SimpleString("prop8");
        short val8 = RandomUtil.randomShort();
        msg.putShortProperty(prop8, val8);
        SimpleString prop9 = new SimpleString("prop9");
        SimpleString val9 = new SimpleString(RandomUtil.randomString());
        msg.putStringProperty(prop9, val9);
        Assert.assertEquals(9, msg.getPropertyNames().size());
        Assert.assertTrue(msg.getPropertyNames().contains(prop1));
        Assert.assertTrue(msg.getPropertyNames().contains(prop2));
        Assert.assertTrue(msg.getPropertyNames().contains(prop3));
        Assert.assertTrue(msg.getPropertyNames().contains(prop4));
        Assert.assertTrue(msg.getPropertyNames().contains(prop5));
        Assert.assertTrue(msg.getPropertyNames().contains(prop6));
        Assert.assertTrue(msg.getPropertyNames().contains(prop7));
        Assert.assertTrue(msg.getPropertyNames().contains(prop8));
        Assert.assertTrue(msg.getPropertyNames().contains(prop9));
        Assert.assertTrue(msg.containsProperty(prop1));
        Assert.assertTrue(msg.containsProperty(prop2));
        Assert.assertTrue(msg.containsProperty(prop3));
        Assert.assertTrue(msg.containsProperty(prop4));
        Assert.assertTrue(msg.containsProperty(prop5));
        Assert.assertTrue(msg.containsProperty(prop6));
        Assert.assertTrue(msg.containsProperty(prop7));
        Assert.assertTrue(msg.containsProperty(prop8));
        Assert.assertTrue(msg.containsProperty(prop9));
        Assert.assertEquals(val1, msg.getObjectProperty(prop1));
        Assert.assertEquals(val2, msg.getObjectProperty(prop2));
        Assert.assertEquals(val3, msg.getObjectProperty(prop3));
        Assert.assertEquals(val4, msg.getObjectProperty(prop4));
        Assert.assertEquals(val5, msg.getObjectProperty(prop5));
        Assert.assertEquals(val6, msg.getObjectProperty(prop6));
        Assert.assertEquals(val7, msg.getObjectProperty(prop7));
        Assert.assertEquals(val8, msg.getObjectProperty(prop8));
        Assert.assertEquals(val9, msg.getObjectProperty(prop9));
        SimpleString val10 = new SimpleString(RandomUtil.randomString());
        // test overwrite
        msg.putStringProperty(prop9, val10);
        Assert.assertEquals(val10, msg.getObjectProperty(prop9));
        int val11 = RandomUtil.randomInt();
        msg.putIntProperty(prop9, val11);
        Assert.assertEquals(val11, msg.getObjectProperty(prop9));
        msg.removeProperty(prop1);
        Assert.assertEquals(8, msg.getPropertyNames().size());
        Assert.assertTrue(msg.getPropertyNames().contains(prop2));
        Assert.assertTrue(msg.getPropertyNames().contains(prop3));
        Assert.assertTrue(msg.getPropertyNames().contains(prop4));
        Assert.assertTrue(msg.getPropertyNames().contains(prop5));
        Assert.assertTrue(msg.getPropertyNames().contains(prop6));
        Assert.assertTrue(msg.getPropertyNames().contains(prop7));
        Assert.assertTrue(msg.getPropertyNames().contains(prop8));
        Assert.assertTrue(msg.getPropertyNames().contains(prop9));
        msg.removeProperty(prop2);
        Assert.assertEquals(7, msg.getPropertyNames().size());
        Assert.assertTrue(msg.getPropertyNames().contains(prop3));
        Assert.assertTrue(msg.getPropertyNames().contains(prop4));
        Assert.assertTrue(msg.getPropertyNames().contains(prop5));
        Assert.assertTrue(msg.getPropertyNames().contains(prop6));
        Assert.assertTrue(msg.getPropertyNames().contains(prop7));
        Assert.assertTrue(msg.getPropertyNames().contains(prop8));
        Assert.assertTrue(msg.getPropertyNames().contains(prop9));
        msg.removeProperty(prop9);
        Assert.assertEquals(6, msg.getPropertyNames().size());
        Assert.assertTrue(msg.getPropertyNames().contains(prop3));
        Assert.assertTrue(msg.getPropertyNames().contains(prop4));
        Assert.assertTrue(msg.getPropertyNames().contains(prop5));
        Assert.assertTrue(msg.getPropertyNames().contains(prop6));
        Assert.assertTrue(msg.getPropertyNames().contains(prop7));
        Assert.assertTrue(msg.getPropertyNames().contains(prop8));
        msg.removeProperty(prop3);
        msg.removeProperty(prop4);
        msg.removeProperty(prop5);
        msg.removeProperty(prop6);
        msg.removeProperty(prop7);
        msg.removeProperty(prop8);
        Assert.assertEquals(0, msg.getPropertyNames().size());
    }
}
Also used : CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) SessionSendMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionSendMessage) ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) Message(org.apache.activemq.artemis.api.core.Message) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessageImpl(org.apache.activemq.artemis.core.client.impl.ClientMessageImpl) Test(org.junit.Test)

Example 4 with ClientMessageImpl

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

the class ManagementHelperTest method testArrayOfStringParameter.

// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testArrayOfStringParameter() throws Exception {
    String resource = RandomUtil.randomString();
    String operationName = RandomUtil.randomString();
    String param = RandomUtil.randomString();
    String[] params = new String[] { RandomUtil.randomString(), RandomUtil.randomString(), RandomUtil.randomString() };
    ClientMessage msg = new ClientMessageImpl((byte) 0, false, 0, 0, (byte) 4, 1000);
    ManagementHelper.putOperationInvocation(msg, resource, operationName, param, params);
    Object[] parameters = ManagementHelper.retrieveOperationParameters(msg);
    Assert.assertEquals(2, parameters.length);
    Assert.assertEquals(param, parameters[0]);
    Object parameter_2 = parameters[1];
    ManagementHelperTest.log.info("type " + parameter_2);
    Assert.assertTrue(parameter_2 instanceof Object[]);
    Object[] retrievedParams = (Object[]) parameter_2;
    Assert.assertEquals(params.length, retrievedParams.length);
    for (int i = 0; i < retrievedParams.length; i++) {
        Assert.assertEquals(params[i], retrievedParams[i]);
    }
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientMessageImpl(org.apache.activemq.artemis.core.client.impl.ClientMessageImpl) Test(org.junit.Test)

Example 5 with ClientMessageImpl

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

the class ManagementHelperTest method testParams.

@Test
public void testParams() throws Exception {
    String resource = RandomUtil.randomString();
    String operationName = RandomUtil.randomString();
    long i = RandomUtil.randomInt();
    String s = RandomUtil.randomString();
    double d = RandomUtil.randomDouble();
    boolean b = RandomUtil.randomBoolean();
    long l = RandomUtil.randomLong();
    Map<String, Object> map = new HashMap<>();
    String key1 = RandomUtil.randomString();
    int value1 = RandomUtil.randomInt();
    String key2 = RandomUtil.randomString();
    double value2 = RandomUtil.randomDouble();
    String key3 = RandomUtil.randomString();
    String value3 = RandomUtil.randomString();
    String key4 = RandomUtil.randomString();
    boolean value4 = RandomUtil.randomBoolean();
    String key5 = RandomUtil.randomString();
    long value5 = RandomUtil.randomLong();
    map.put(key1, value1);
    map.put(key2, value2);
    map.put(key3, value3);
    map.put(key4, value4);
    map.put(key5, value5);
    Map<String, Object> map2 = new HashMap<>();
    String key2_1 = RandomUtil.randomString();
    int value2_1 = RandomUtil.randomInt();
    String key2_2 = RandomUtil.randomString();
    double value2_2 = RandomUtil.randomDouble();
    String key2_3 = RandomUtil.randomString();
    String value2_3 = RandomUtil.randomString();
    String key2_4 = RandomUtil.randomString();
    boolean value2_4 = RandomUtil.randomBoolean();
    String key2_5 = RandomUtil.randomString();
    long value2_5 = RandomUtil.randomLong();
    map2.put(key2_1, value2_1);
    map2.put(key2_2, value2_2);
    map2.put(key2_3, value2_3);
    map2.put(key2_4, value2_4);
    map2.put(key2_5, value2_5);
    Map<String, Object> map3 = new HashMap<>();
    String key3_1 = RandomUtil.randomString();
    int value3_1 = RandomUtil.randomInt();
    String key3_2 = RandomUtil.randomString();
    double value3_2 = RandomUtil.randomDouble();
    String key3_3 = RandomUtil.randomString();
    String value3_3 = RandomUtil.randomString();
    String key3_4 = RandomUtil.randomString();
    boolean value3_4 = RandomUtil.randomBoolean();
    String key3_5 = RandomUtil.randomString();
    long value3_5 = RandomUtil.randomLong();
    map3.put(key3_1, value3_1);
    map3.put(key3_2, value3_2);
    map3.put(key3_3, value3_3);
    map3.put(key3_4, value3_4);
    map3.put(key3_5, value3_5);
    Map[] maps = new Map[] { map2, map3 };
    String strElem0 = RandomUtil.randomString();
    String strElem1 = RandomUtil.randomString();
    String strElem2 = RandomUtil.randomString();
    String[] strArray = new String[] { strElem0, strElem1, strElem2 };
    Object[] params = new Object[] { i, s, d, b, l, map, strArray, maps };
    ClientMessageImpl msg = new ClientMessageImpl((byte) 0, false, 0, 0, (byte) 4, 1000);
    ManagementHelper.putOperationInvocation(msg, resource, operationName, params);
    Object[] parameters = ManagementHelper.retrieveOperationParameters(msg);
    Assert.assertEquals(params.length, parameters.length);
    Assert.assertEquals(i, parameters[0]);
    Assert.assertEquals(s, parameters[1]);
    Assert.assertEquals(d, parameters[2]);
    Assert.assertEquals(b, parameters[3]);
    Assert.assertEquals(l, parameters[4]);
    Map mapRes = (Map) parameters[5];
    Assert.assertEquals(map.size(), mapRes.size());
    Assert.assertEquals((long) value1, mapRes.get(key1));
    Assert.assertEquals(value2, mapRes.get(key2));
    Assert.assertEquals(value3, mapRes.get(key3));
    Assert.assertEquals(value4, mapRes.get(key4));
    Assert.assertEquals(value5, mapRes.get(key5));
    Object[] strArr2 = (Object[]) parameters[6];
    Assert.assertEquals(strArray.length, strArr2.length);
    Assert.assertEquals(strElem0, strArr2[0]);
    Assert.assertEquals(strElem1, strArr2[1]);
    Assert.assertEquals(strElem2, strArr2[2]);
    Object[] mapArray = (Object[]) parameters[7];
    Assert.assertEquals(2, mapArray.length);
    Map mapRes2 = (Map) mapArray[0];
    Assert.assertEquals(map2.size(), mapRes2.size());
    Assert.assertEquals((long) value2_1, mapRes2.get(key2_1));
    Assert.assertEquals(value2_2, mapRes2.get(key2_2));
    Assert.assertEquals(value2_3, mapRes2.get(key2_3));
    Assert.assertEquals(value2_4, mapRes2.get(key2_4));
    Assert.assertEquals(value2_5, mapRes2.get(key2_5));
    Map mapRes3 = (Map) mapArray[1];
    Assert.assertEquals(map3.size(), mapRes3.size());
    Assert.assertEquals((long) value3_1, mapRes3.get(key3_1));
    Assert.assertEquals(value3_2, mapRes3.get(key3_2));
    Assert.assertEquals(value3_3, mapRes3.get(key3_3));
    Assert.assertEquals(value3_4, mapRes3.get(key3_4));
    Assert.assertEquals(value3_5, mapRes3.get(key3_5));
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) ClientMessageImpl(org.apache.activemq.artemis.core.client.impl.ClientMessageImpl) Test(org.junit.Test)

Aggregations

ClientMessageImpl (org.apache.activemq.artemis.core.client.impl.ClientMessageImpl)17 Test (org.junit.Test)16 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)8 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)8 ICoreMessage (org.apache.activemq.artemis.api.core.ICoreMessage)4 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BytesMessage (javax.jms.BytesMessage)2 MapMessage (javax.jms.MapMessage)2 ObjectMessage (javax.jms.ObjectMessage)2 StreamMessage (javax.jms.StreamMessage)2 TextMessage (javax.jms.TextMessage)2 Message (org.apache.activemq.artemis.api.core.Message)2 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)2 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)2 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)2 SessionSendMessage (org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionSendMessage)2 ActiveMQBytesMessage (org.apache.activemq.artemis.jms.client.ActiveMQBytesMessage)2 ActiveMQMapMessage (org.apache.activemq.artemis.jms.client.ActiveMQMapMessage)2