Search in sources :

Example 1 with UnsignedInteger

use of org.apache.qpid.proton.amqp.UnsignedInteger in project activemq-artemis by apache.

the class CoreAmqpConverter method fromCore.

public static AMQPMessage fromCore(ICoreMessage coreMessage) throws Exception {
    if (coreMessage == null) {
        return null;
    }
    ServerJMSMessage message = ServerJMSMessage.wrapCoreMessage(coreMessage);
    message.decode();
    long messageFormat = 0;
    Header header = null;
    final Properties properties = new Properties();
    Map<Symbol, Object> daMap = null;
    final Map<Symbol, Object> maMap = new HashMap<>();
    Map<String, Object> apMap = null;
    Map<Object, Object> footerMap = null;
    Section body = convertBody(message, maMap, properties);
    if (message.getInnerMessage().isDurable()) {
        if (header == null) {
            header = new Header();
        }
        header.setDurable(true);
    }
    byte priority = (byte) message.getJMSPriority();
    if (priority != javax.jms.Message.DEFAULT_PRIORITY) {
        if (header == null) {
            header = new Header();
        }
        header.setPriority(UnsignedByte.valueOf(priority));
    }
    String type = message.getJMSType();
    if (type != null) {
        properties.setSubject(type);
    }
    String messageId = message.getJMSMessageID();
    if (messageId != null) {
        try {
            properties.setMessageId(AMQPMessageIdHelper.INSTANCE.toIdObject(messageId));
        } catch (ActiveMQAMQPIllegalStateException e) {
            properties.setMessageId(messageId);
        }
    }
    Destination destination = message.getJMSDestination();
    if (destination != null) {
        properties.setTo(toAddress(destination));
        maMap.put(JMS_DEST_TYPE_MSG_ANNOTATION, destinationType(destination));
    }
    Destination replyTo = message.getJMSReplyTo();
    if (replyTo != null) {
        properties.setReplyTo(toAddress(replyTo));
        maMap.put(JMS_REPLY_TO_TYPE_MSG_ANNOTATION, destinationType(replyTo));
    }
    String correlationId = message.getJMSCorrelationID();
    if (correlationId != null) {
        try {
            properties.setCorrelationId(AMQPMessageIdHelper.INSTANCE.toIdObject(correlationId));
        } catch (ActiveMQAMQPIllegalStateException e) {
            properties.setCorrelationId(correlationId);
        }
    }
    long expiration = message.getJMSExpiration();
    if (expiration != 0) {
        long ttl = expiration - System.currentTimeMillis();
        if (ttl < 0) {
            ttl = 1;
        }
        if (header == null) {
            header = new Header();
        }
        header.setTtl(new UnsignedInteger((int) ttl));
        properties.setAbsoluteExpiryTime(new Date(expiration));
    }
    long timeStamp = message.getJMSTimestamp();
    if (timeStamp != 0) {
        properties.setCreationTime(new Date(timeStamp));
    }
    final Set<String> keySet = MessageUtil.getPropertyNames(message.getInnerMessage());
    for (String key : keySet) {
        if (key.startsWith("JMSX")) {
            if (key.equals("JMSXUserID")) {
                String value = message.getStringProperty(key);
                properties.setUserId(new Binary(value.getBytes(StandardCharsets.UTF_8)));
                continue;
            } else if (key.equals("JMSXGroupID")) {
                String value = message.getStringProperty(key);
                properties.setGroupId(value);
                continue;
            } else if (key.equals("JMSXGroupSeq")) {
                UnsignedInteger value = new UnsignedInteger(message.getIntProperty(key));
                properties.setGroupSequence(value);
                continue;
            }
        } else if (key.startsWith(JMS_AMQP_PREFIX)) {
            // AMQP Message Information stored from a conversion to the Core Message
            if (key.equals(JMS_AMQP_NATIVE)) {
                // skip..internal use only
                continue;
            } else if (key.equals(JMS_AMQP_FIRST_ACQUIRER)) {
                if (header == null) {
                    header = new Header();
                }
                header.setFirstAcquirer(message.getBooleanProperty(key));
                continue;
            } else if (key.equals(JMS_AMQP_HEADER)) {
                if (header == null) {
                    header = new Header();
                }
                continue;
            } else if (key.equals(JMS_AMQP_HEADER_DURABLE)) {
                if (header == null) {
                    header = new Header();
                }
                header.setDurable(message.getInnerMessage().isDurable());
                continue;
            } else if (key.equals(JMS_AMQP_HEADER_PRIORITY)) {
                if (header == null) {
                    header = new Header();
                }
                header.setPriority(UnsignedByte.valueOf(priority));
                continue;
            } else if (key.startsWith(JMS_AMQP_PROPERTIES)) {
                continue;
            } else if (key.startsWith(JMS_AMQP_DELIVERY_ANNOTATION_PREFIX)) {
                if (daMap == null) {
                    daMap = new HashMap<>();
                }
                String name = key.substring(JMS_AMQP_DELIVERY_ANNOTATION_PREFIX.length());
                daMap.put(Symbol.valueOf(name), message.getObjectProperty(key));
                continue;
            } else if (key.startsWith(JMS_AMQP_MESSAGE_ANNOTATION_PREFIX)) {
                String name = key.substring(JMS_AMQP_MESSAGE_ANNOTATION_PREFIX.length());
                maMap.put(Symbol.valueOf(name), message.getObjectProperty(key));
                continue;
            } else if (key.equals(JMS_AMQP_CONTENT_TYPE)) {
                properties.setContentType(Symbol.getSymbol(message.getStringProperty(key)));
                continue;
            } else if (key.equals(JMS_AMQP_CONTENT_ENCODING)) {
                properties.setContentEncoding(Symbol.getSymbol(message.getStringProperty(key)));
                continue;
            } else if (key.equals(JMS_AMQP_REPLYTO_GROUP_ID)) {
                properties.setReplyToGroupId(message.getStringProperty(key));
                continue;
            } else if (key.startsWith(JMS_AMQP_FOOTER_PREFIX)) {
                if (footerMap == null) {
                    footerMap = new HashMap<>();
                }
                String name = key.substring(JMS_AMQP_FOOTER_PREFIX.length());
                footerMap.put(name, message.getObjectProperty(key));
                continue;
            }
        } else if (key.equals("_AMQ_GROUP_ID")) {
            String value = message.getStringProperty(key);
            properties.setGroupId(value);
            continue;
        } else if (key.equals(NATIVE_MESSAGE_ID)) {
            // skip..internal use only
            continue;
        } else if (key.endsWith(HDR_SCHEDULED_DELIVERY_TIME.toString())) {
            // skip..remove annotation from previous inbound transformation
            continue;
        }
        if (apMap == null) {
            apMap = new HashMap<>();
        }
        Object objectProperty = message.getObjectProperty(key);
        if (objectProperty instanceof byte[]) {
            objectProperty = new Binary((byte[]) objectProperty);
        }
        apMap.put(key, objectProperty);
    }
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(1024);
    try {
        EncoderImpl encoder = TLSEncode.getEncoder();
        encoder.setByteBuffer(new NettyWritable(buffer));
        if (header != null) {
            encoder.writeObject(header);
        }
        if (daMap != null) {
            encoder.writeObject(new DeliveryAnnotations(daMap));
        }
        if (maMap != null) {
            encoder.writeObject(new MessageAnnotations(maMap));
        }
        if (properties != null) {
            encoder.writeObject(properties);
        }
        if (apMap != null) {
            encoder.writeObject(new ApplicationProperties(apMap));
        }
        if (body != null) {
            encoder.writeObject(body);
        }
        if (footerMap != null) {
            encoder.writeObject(new Footer(footerMap));
        }
        byte[] data = new byte[buffer.writerIndex()];
        buffer.readBytes(data);
        AMQPMessage amqpMessage = new AMQPMessage(messageFormat, data);
        amqpMessage.setMessageID(message.getInnerMessage().getMessageID());
        amqpMessage.setReplyTo(coreMessage.getReplyTo());
        return amqpMessage;
    } finally {
        TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null);
        buffer.release();
    }
}
Also used : Destination(javax.jms.Destination) EncoderImpl(org.apache.qpid.proton.codec.EncoderImpl) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) ByteBuf(io.netty.buffer.ByteBuf) NettyWritable(org.apache.activemq.artemis.protocol.amqp.util.NettyWritable) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) ActiveMQAMQPIllegalStateException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPIllegalStateException) DeliveryAnnotations(org.apache.qpid.proton.amqp.messaging.DeliveryAnnotations) Section(org.apache.qpid.proton.amqp.messaging.Section) AMQPMessage(org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage) Date(java.util.Date) Header(org.apache.qpid.proton.amqp.messaging.Header) Footer(org.apache.qpid.proton.amqp.messaging.Footer) Binary(org.apache.qpid.proton.amqp.Binary) UnsignedInteger(org.apache.qpid.proton.amqp.UnsignedInteger) ServerJMSMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMessage)

Example 2 with UnsignedInteger

use of org.apache.qpid.proton.amqp.UnsignedInteger in project activemq-artemis by apache.

the class AMQPToOpenwireTest method testDeliveryCountMessage.

@Test
public void testDeliveryCountMessage() throws Exception {
    AmqpClient client = new AmqpClient(new URI("tcp://127.0.0.1:61616"), null, null);
    AmqpConnection amqpconnection = client.connect();
    try {
        AmqpSession session = amqpconnection.createSession();
        AmqpSender sender = session.createSender(queueName);
        AmqpMessage message = new AmqpMessage();
        message.setMessageId("MessageID:" + 0);
        message.getWrappedMessage().setHeader(new Header());
        message.getWrappedMessage().getHeader().setDeliveryCount(new UnsignedInteger(2));
        sender.send(message);
    } finally {
        amqpconnection.close();
    }
    Connection connection = null;
    try {
        connection = factory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(queueName);
        MessageConsumer consumer = session.createConsumer(queue);
        connection.start();
        Message receive = consumer.receive(5000);
        assertNotNull(receive);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) MessageConsumer(javax.jms.MessageConsumer) ObjectMessage(javax.jms.ObjectMessage) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(javax.jms.Message) BytesMessage(javax.jms.BytesMessage) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) Connection(javax.jms.Connection) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) URI(java.net.URI) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) Header(org.apache.qpid.proton.amqp.messaging.Header) UnsignedInteger(org.apache.qpid.proton.amqp.UnsignedInteger) Queue(javax.jms.Queue) Session(javax.jms.Session) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) Test(org.junit.Test)

Example 3 with UnsignedInteger

use of org.apache.qpid.proton.amqp.UnsignedInteger in project activemq-artemis by apache.

the class AMQPToJMSCoreTest method testMessageDestination.

@Test
public void testMessageDestination() throws Exception {
    System.out.println("foo");
    AmqpClient client = new AmqpClient(new URI("tcp://127.0.0.1:61616"), null, null);
    AmqpConnection amqpconnection = client.connect();
    try {
        AmqpSession session = amqpconnection.createSession();
        AmqpSender sender = session.createSender(queueName);
        AmqpMessage message = new AmqpMessage();
        message.setMessageId("MessageID:" + 0);
        // message.setApplicationProperty("_AMQ_ROUTING_TYPE", (byte) 1);
        message.getWrappedMessage().setHeader(new Header());
        message.getWrappedMessage().getHeader().setDeliveryCount(new UnsignedInteger(2));
        sender.send(message);
    } finally {
        amqpconnection.close();
    }
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
    Connection connection = null;
    try {
        connection = factory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(ActiveMQJMSClient.createQueue(queueName));
        connection.start();
        Message message = consumer.receive(2000);
        Assert.assertNotNull(message);
        ActiveMQDestination jmsDestination = (ActiveMQDestination) message.getJMSDestination();
        Assert.assertEquals(queueName, jmsDestination.getAddress());
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) MessageConsumer(javax.jms.MessageConsumer) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(javax.jms.Message) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) Connection(javax.jms.Connection) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) URI(java.net.URI) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) ActiveMQDestination(org.apache.activemq.artemis.jms.client.ActiveMQDestination) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) Header(org.apache.qpid.proton.amqp.messaging.Header) UnsignedInteger(org.apache.qpid.proton.amqp.UnsignedInteger) Session(javax.jms.Session) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) Test(org.junit.Test)

Example 4 with UnsignedInteger

use of org.apache.qpid.proton.amqp.UnsignedInteger in project activemq-artemis by apache.

the class AmqpReceiverWithFiltersTest method testReceivedUnsignedFilter.

@Test(timeout = 60000)
public void testReceivedUnsignedFilter() throws Exception {
    final int NUM_MESSAGES = 100;
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = client.connect();
    try {
        // Normal Session which won't create an TXN itself
        AmqpSession session = connection.createSession();
        AmqpSender sender = session.createSender(getQueueName());
        for (int i = 0; i < NUM_MESSAGES + 1; ++i) {
            AmqpMessage message = new AmqpMessage();
            message.setText("Test-Message");
            message.setApplicationProperty("myNewID", new UnsignedInteger(i));
            sender.send(message);
        }
        // Read all messages from the Queue, do not accept them yet.
        AmqpReceiver receiver = session.createReceiver(getQueueName(), "myNewID < " + (NUM_MESSAGES / 2));
        ArrayList<AmqpMessage> messages = new ArrayList<>(NUM_MESSAGES);
        receiver.flow((NUM_MESSAGES + 2) * 2);
        for (int i = 0; i < NUM_MESSAGES / 2; ++i) {
            AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
            Assert.assertNotNull(message);
            System.out.println("Read message: " + message.getApplicationProperty("myNewID"));
            assertNotNull(message);
            messages.add(message);
        }
        Assert.assertNull(receiver.receiveNoWait());
    } finally {
        connection.close();
    }
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) ArrayList(java.util.ArrayList) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) UnsignedInteger(org.apache.qpid.proton.amqp.UnsignedInteger) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Example 5 with UnsignedInteger

use of org.apache.qpid.proton.amqp.UnsignedInteger in project activemq-artemis by apache.

the class AmqpManagementTest method testUnsignedValues.

/**
 * Some clients use Unsigned types from org.apache.qpid.proton.amqp
 * @throws Exception
 */
@Test(timeout = 60000)
public void testUnsignedValues() throws Exception {
    int sequence = 42;
    LinkedHashMap<String, Object> map = new LinkedHashMap<>();
    map.put("sequence", new UnsignedInteger(sequence));
    ServerJMSMapMessage msg = createMapMessage(1, map, null);
    assertEquals(msg.getInt("sequence"), sequence);
    map.clear();
    map.put("sequence", new UnsignedLong(sequence));
    msg = createMapMessage(1, map, null);
    assertEquals(msg.getLong("sequence"), sequence);
    map.clear();
    map.put("sequence", new UnsignedShort((short) sequence));
    msg = createMapMessage(1, map, null);
    assertEquals(msg.getShort("sequence"), sequence);
    map.clear();
    map.put("sequence", new UnsignedByte((byte) sequence));
    msg = createMapMessage(1, map, null);
    assertEquals(msg.getByte("sequence"), sequence);
}
Also used : UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) ServerJMSMapMessage(org.apache.activemq.artemis.protocol.amqp.converter.jms.ServerJMSMapMessage) UnsignedInteger(org.apache.qpid.proton.amqp.UnsignedInteger) UnsignedByte(org.apache.qpid.proton.amqp.UnsignedByte) LinkedHashMap(java.util.LinkedHashMap) UnsignedShort(org.apache.qpid.proton.amqp.UnsignedShort) Test(org.junit.Test)

Aggregations

UnsignedInteger (org.apache.qpid.proton.amqp.UnsignedInteger)9 Test (org.junit.Test)6 Header (org.apache.qpid.proton.amqp.messaging.Header)5 HashMap (java.util.HashMap)3 AMQPMessage (org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage)3 AmqpClient (org.apache.activemq.transport.amqp.client.AmqpClient)3 AmqpConnection (org.apache.activemq.transport.amqp.client.AmqpConnection)3 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)3 AmqpSender (org.apache.activemq.transport.amqp.client.AmqpSender)3 AmqpSession (org.apache.activemq.transport.amqp.client.AmqpSession)3 UnsignedByte (org.apache.qpid.proton.amqp.UnsignedByte)3 UnsignedLong (org.apache.qpid.proton.amqp.UnsignedLong)3 UnsignedShort (org.apache.qpid.proton.amqp.UnsignedShort)3 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)3 Properties (org.apache.qpid.proton.amqp.messaging.Properties)3 URI (java.net.URI)2 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 Connection (javax.jms.Connection)2 Message (javax.jms.Message)2