Search in sources :

Example 11 with AMQPMessage

use of com.swiftmq.amqp.v100.messaging.AMQPMessage in project swiftmq-client by iitsoftware.

the class EndpointImpl method poll.

public void poll() {
    try {
        AMQPMessage msg = consumer.receiveNoWait(this);
        if (msg != null) {
            Data data = msg.getData().get(0);
            dis.reset();
            dis.setBuffer(data.getValue());
            Dumpable d = Dumpalizer.construct(dis, factory);
            if (d instanceof Reply)
                requestRegistry.setReply((Reply) d);
            else
                dispatch((Request) d);
            pollerService.execute(poller);
        }
    } catch (Exception e) {
        e.printStackTrace();
        close();
    }
}
Also used : Data(com.swiftmq.amqp.v100.generated.messaging.message_format.Data) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) Dumpable(com.swiftmq.tools.dump.Dumpable)

Example 12 with AMQPMessage

use of com.swiftmq.amqp.v100.messaging.AMQPMessage in project swiftmq-client by iitsoftware.

the class EndpointImpl method performRequest.

public synchronized void performRequest(Request request) {
    try {
        dos.rewind();
        Dumpalizer.dump(dos, request);
        AMQPMessage msg = new AMQPMessage();
        byte[] bytes = new byte[dos.getCount()];
        System.arraycopy(dos.getBuffer(), 0, bytes, 0, bytes.length);
        msg.addData(new Data(bytes));
        Properties prop = new Properties();
        if (connection.getUserName() == null)
            prop.setUserId(new AMQPBinary("anonymous".getBytes()));
        else
            prop.setUserId(new AMQPBinary(connection.getUserName().getBytes()));
        prop.setReplyTo(replyAddress);
        msg.setProperties(prop);
        producer.send(msg);
    } catch (Exception e) {
        close();
    }
}
Also used : Data(com.swiftmq.amqp.v100.generated.messaging.message_format.Data) AMQPBinary(com.swiftmq.amqp.v100.types.AMQPBinary) Properties(com.swiftmq.amqp.v100.generated.messaging.message_format.Properties) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage)

Example 13 with AMQPMessage

use of com.swiftmq.amqp.v100.messaging.AMQPMessage in project swiftmq-ce by iitsoftware.

the class TargetLink method handleTransactionRequest.

private void handleTransactionRequest(TransferFrame frame) throws EndWithErrorException {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/handleTransactionRequest, frame=" + frame);
    AMQPMessage msg = null;
    try {
        msg = new AMQPMessage(frame.getPayload());
    } catch (Exception e) {
        e.printStackTrace();
        new SessionEndException(AmqpError.DECODE_ERROR, new AMQPString("Exception during decode of a message: " + e));
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/handleTransactionRequest, msg=" + msg);
    if (msg.getAmqpValue() == null)
        throw new SessionEndException(AmqpError.NOT_FOUND, new AMQPString("Missing amqp-value message body in transaction request!"));
    AMQPType bare = msg.getAmqpValue().getValue();
    AMQPDescribedConstructor constructor = bare.getConstructor();
    if (constructor == null)
        throw new SessionEndException(AmqpError.DECODE_ERROR, new AMQPString("Missing constructor: " + bare));
    if (!AMQPTypeDecoder.isList(constructor.getFormatCode()))
        throw new SessionEndException(AmqpError.DECODE_ERROR, new AMQPString("Message body is not of a list type, code=0x" + Integer.toHexString(bare.getCode()) + ", bare=" + bare));
    AMQPType descriptor = constructor.getDescriptor();
    int code = descriptor.getCode();
    try {
        if (AMQPTypeDecoder.isULong(code)) {
            long type = ((AMQPUnsignedLong) descriptor).getValue();
            if (type == Declare.DESCRIPTOR_CODE)
                handleDeclare(new Declare(((AMQPList) bare).getValue()));
            else if (type == Discharge.DESCRIPTOR_CODE)
                handleDischarge(new Discharge(((AMQPList) bare).getValue()));
            else
                throw new SessionEndException(AmqpError.DECODE_ERROR, new AMQPString("Invalid descriptor type: " + type + ", bare=" + bare));
        } else if (AMQPTypeDecoder.isSymbol(code)) {
            String type = ((AMQPSymbol) descriptor).getValue();
            if (type.equals(Declare.DESCRIPTOR_NAME))
                handleDeclare(new Declare(((AMQPList) bare).getValue()));
            else if (type.equals(Discharge.DESCRIPTOR_NAME))
                handleDischarge(new Discharge(((AMQPList) bare).getValue()));
            else
                throw new SessionEndException(AmqpError.DECODE_ERROR, new AMQPString("Invalid descriptor type: " + type + ", bare=" + bare));
        } else
            throw new SessionEndException(AmqpError.DECODE_ERROR, new AMQPString("Invalid type of constructor descriptor (actual type=" + code + ", expected=symbold or ulong), bare= " + bare));
    } catch (Exception e) {
        e.printStackTrace();
        throw new SessionEndException(AmqpError.DECODE_ERROR, new AMQPString(e.toString()));
    }
}
Also used : AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) AuthenticationException(com.swiftmq.swiftlet.auth.AuthenticationException) QueueException(com.swiftmq.swiftlet.queue.QueueException) InvalidSelectorException(javax.jms.InvalidSelectorException) TopicException(com.swiftmq.swiftlet.topic.TopicException)

Example 14 with AMQPMessage

use of com.swiftmq.amqp.v100.messaging.AMQPMessage in project swiftmq-ce by iitsoftware.

the class JMSMappingOutboundTransformer method transform.

public void transform(Delivery delivery) throws AMQPException, JMSException {
    jmsTypeProp = getValue(PROP_JMS_TYPE, null);
    MessageImpl message = delivery.getMessage();
    if (message.propertyExists(amqpNative) && message.getBooleanProperty(amqpNative))
        throw new JMSException("Message is an AMQP native transformation");
    try {
        AMQPMessage amqpMessage = new AMQPMessage();
        Header header = new Header();
        int deliveryMode = message.getJMSDeliveryMode();
        header.setDurable(new AMQPBoolean(deliveryMode == DeliveryMode.PERSISTENT));
        header.setPriority(new AMQPUnsignedByte(message.getJMSPriority()));
        header.setDeliveryCount(new AMQPUnsignedInt(delivery.getMessageIndex().getDeliveryCount() - 1));
        long ttl = message.getJMSExpiration();
        if (ttl > 0) {
            if (message.propertyExists(Util.PROP_EXPIRATION_CURRENT_TIME_ADD))
                header.setTtl(new Milliseconds(ttl - message.getLongProperty(Util.PROP_EXPIRATION_CURRENT_TIME_ADD)));
        }
        Properties properties = new Properties();
        String messageId = message.getJMSMessageID();
        if (messageId != null)
            properties.setMessageId(new MessageIdString(message.getJMSMessageID()));
        if (message.propertyExists(Util.PROP_AMQP_TO_ADDRESS))
            properties.setTo(new AddressString(message.getStringProperty(Util.PROP_AMQP_TO_ADDRESS)));
        else
            properties.setTo(new AddressString(message.getJMSDestination().toString()));
        Destination replyTo = message.getJMSReplyTo();
        if (replyTo != null)
            properties.setReplyTo(new AddressString(replyTo.toString()));
        String correlationId = message.getJMSCorrelationID();
        if (correlationId != null)
            properties.setCorrelationId(new MessageIdString(correlationId));
        if (ttl > 0)
            properties.setAbsoluteExpiryTime(new AMQPTimestamp(ttl));
        long timestamp = message.getJMSTimestamp();
        if (timestamp > 0)
            properties.setCreationTime(new AMQPTimestamp(timestamp));
        Map daMap = null;
        Map maMap = null;
        Map ftMap = null;
        Map apMap = null;
        String firstAcquirerName = prefixVendor + "FirstAcquirer";
        String subject = prefixVendor + "Subject";
        String contentType = prefixVendor + "ContentType";
        String contentEncoding = prefixVendor + "ContentEncoding";
        String replyToGroupId = prefixVendor + "ReplyToGroupID";
        for (Enumeration _enum = message.getPropertyNames(); _enum.hasMoreElements(); ) {
            String name = (String) _enum.nextElement();
            if (name.equals(amqpNative) || name.equals(messageFormat) || name.equals(Util.PROP_EXPIRATION_CURRENT_TIME_ADD) || name.equals(Util.PROP_AMQP_TO_ADDRESS))
                continue;
            if (name.equals(firstAcquirerName))
                header.setFirstAcquirer(new AMQPBoolean(message.getBooleanProperty(firstAcquirerName)));
            else if (name.equals(MessageImpl.PROP_USER_ID))
                properties.setUserId(new AMQPBinary(message.getStringProperty(MessageImpl.PROP_USER_ID).getBytes()));
            else if (name.equals(subject))
                properties.setSubject(new AMQPString(message.getStringProperty(subject)));
            else if (name.equals(contentType))
                properties.setContentType(new AMQPSymbol(message.getStringProperty(contentType)));
            else if (name.equals(contentEncoding))
                properties.setContentEncoding(new AMQPSymbol(message.getStringProperty(contentEncoding)));
            else if (name.equals("JMSXGroupID") && message.getObjectProperty(Util.PROP_GROUP_ID) instanceof String)
                properties.setGroupId(new AMQPString(message.getStringProperty(Util.PROP_GROUP_ID)));
            else if (name.equals("JMSXGroupSeq") && message.getObjectProperty(Util.PROP_GROUP_SEQ) instanceof Long)
                properties.setGroupSequence(new SequenceNo(message.getLongProperty(Util.PROP_GROUP_SEQ)));
            else if (name.equals(replyToGroupId))
                properties.setReplyToGroupId(new AMQPString(message.getStringProperty(replyToGroupId)));
            else if (name.startsWith(prefixDA)) {
                if (daMap == null)
                    daMap = new HashMap();
                daMap.put(new AMQPSymbol(nameTranslator.translate(name.substring(prefixDA.length()))), Util.convertJMStoAMQP(message.getObjectProperty(name)));
            } else if (name.startsWith(prefixMA)) {
                if (maMap == null)
                    maMap = new HashMap();
                maMap.put(new AMQPSymbol(nameTranslator.translate(name.substring(prefixMA.length()))), Util.convertJMStoAMQP(message.getObjectProperty(name)));
            } else if (name.startsWith(prefixFT)) {
                if (ftMap == null)
                    ftMap = new HashMap();
                ftMap.put(new AMQPSymbol(nameTranslator.translate(name.substring(prefixFT.length()))), Util.convertJMStoAMQP(message.getObjectProperty(name)));
            } else {
                if (apMap == null)
                    apMap = new HashMap();
                apMap.put(new AMQPString(nameTranslator.translate(name)), Util.convertJMStoAMQP(message.getObjectProperty(name)));
            }
        }
        if (message.getJMSType() != null) {
            if (jmsTypeProp != null) {
                if (apMap == null)
                    apMap = new HashMap();
                apMap.put(new AMQPString(jmsTypeProp), Util.convertJMStoAMQP(message.getJMSType()));
            } else {
                if (maMap == null)
                    maMap = new HashMap();
                maMap.put(new AMQPSymbol("x-opt-jms-type"), new AMQPString(message.getJMSType()));
            }
        }
        amqpMessage.setHeader(header);
        amqpMessage.setProperties(properties);
        if (daMap != null)
            amqpMessage.setDeliveryAnnotations(new DeliveryAnnotations(daMap));
        if (maMap != null)
            amqpMessage.setMessageAnnotations(new MessageAnnotations(maMap));
        if (apMap != null)
            amqpMessage.setApplicationProperties(new ApplicationProperties(apMap));
        if (ftMap != null)
            amqpMessage.setFooter(new Footer(ftMap));
        bodyFactory.createBody(message, amqpMessage);
        delivery.setAmqpMessage(amqpMessage);
        dbos.rewind();
        amqpMessage.writeContent(dbos);
        byte[] payload = new byte[dbos.getCount()];
        System.arraycopy(dbos.getBuffer(), 0, payload, 0, dbos.getCount());
        delivery.setData(payload);
        delivery.setMessageFormat(0);
    } catch (IOException e) {
        e.printStackTrace();
        throw new AMQPException(e.toString());
    }
}
Also used : Destination(javax.jms.Destination) SequenceNo(com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo) AMQPException(com.swiftmq.amqp.v100.client.AMQPException) HashMap(java.util.HashMap) JMSException(javax.jms.JMSException) Enumeration(java.util.Enumeration) IOException(java.io.IOException) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) Milliseconds(com.swiftmq.amqp.v100.generated.transport.definitions.Milliseconds) MessageImpl(com.swiftmq.jms.MessageImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with AMQPMessage

use of com.swiftmq.amqp.v100.messaging.AMQPMessage in project swiftmq-ce by iitsoftware.

the class Replier method serviceRequests.

public void serviceRequests() {
    try {
        for (int i = 0; i < nMsgs; i++) {
            TxnIdIF txnIdIF = txc.createTxnId();
            AMQPMessage request = consumer.receive();
            request.setTxnIdIF(txnIdIF);
            if (request != null) {
                messageFactory.verify(request);
                if (!request.isSettled())
                    request.accept();
                Properties prop = request.getProperties();
                if (prop == null)
                    throw new Exception("Properties not set in request: " + request);
                AddressIF replyTo = prop.getReplyTo();
                if (replyTo == null)
                    throw new Exception("replyTo not set in request: " + request);
                Producer p = getSession().createProducer(replyTo.getValueString(), qos);
                AMQPMessage reply = messageFactory.createReplyMessage(request);
                reply.setTxnIdIF(txnIdIF);
                Properties prop2 = new Properties();
                prop2.setTo(replyTo);
                prop2.setCorrelationId(prop.getMessageId());
                reply.setProperties(prop2);
                p.send(reply);
                p.close();
            } else
                throw new Exception("Msg == null at i=" + i);
            txc.commit(txnIdIF);
        }
    } catch (Exception e) {
        fail("test failed: " + e);
    }
}
Also used : TxnIdIF(com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF) AddressIF(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressIF) Producer(com.swiftmq.amqp.v100.client.Producer) Properties(com.swiftmq.amqp.v100.generated.messaging.message_format.Properties) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage)

Aggregations

AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)56 AMQPString (com.swiftmq.amqp.v100.types.AMQPString)24 AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)23 TxnIdIF (com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF)20 Properties (com.swiftmq.amqp.v100.generated.messaging.message_format.Properties)19 Map (java.util.Map)13 AddressIF (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressIF)12 HashMap (java.util.HashMap)10 AMQPContext (com.swiftmq.amqp.AMQPContext)9 Data (com.swiftmq.amqp.v100.generated.messaging.message_format.Data)8 AMQPMap (com.swiftmq.amqp.v100.types.AMQPMap)8 JSSESocketFactory (com.swiftmq.net.JSSESocketFactory)8 IOException (java.io.IOException)8 Producer (com.swiftmq.amqp.v100.client.Producer)6 ApplicationProperties (com.swiftmq.amqp.v100.generated.messaging.message_format.ApplicationProperties)6 AMQPBoolean (com.swiftmq.amqp.v100.types.AMQPBoolean)5 List (java.util.List)4 AMQPException (com.swiftmq.amqp.v100.client.AMQPException)3 Connection (com.swiftmq.amqp.v100.client.Connection)3 Session (com.swiftmq.amqp.v100.client.Session)3