Search in sources :

Example 56 with AMQPMessage

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

the class AMQPValueStringMessageFactory method verify.

public void verify(AMQPMessage message) throws Exception {
    AmqpValue value = message.getAmqpValue();
    if (value == null)
        throw new Exception(("verify - no AmqpValue section found!"));
    AMQPString s = (AMQPString) value.getValue();
    if (!s.getValue().startsWith("Message #"))
        throw new Exception("verify - invalid value detected: " + s.getValue());
}
Also used : AMQPString(com.swiftmq.amqp.v100.types.AMQPString) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)

Example 57 with AMQPMessage

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

the class Consumer method receive.

private AMQPMessage receive(long timeout, MessageAvailabilityListener messageAvailabilityListener) {
    AMQPMessage msg = null;
    if (closed)
        return null;
    try {
        cacheLock.lock();
        if (cache.size() == 0) {
            if (timeout > 0) {
                try {
                    cacheEmpty.await(timeout, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                }
            } else {
                if (timeout == 0)
                    cacheEmpty.awaitUninterruptibly();
                else if (timeout == -1)
                    this.messageAvailabilityListener = messageAvailabilityListener;
            }
        }
        if (cache.size() == 0)
            return null;
        msg = (AMQPMessage) cache.remove(0);
        msg.setConsumer(this);
        long dc = deliveryCount.incrementAndGet();
        currentLinkCredit--;
        if (!acquireMode && currentLinkCredit == 0)
            fillCache(dc);
    } finally {
        cacheLock.unlock();
    }
    return msg;
}
Also used : AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage)

Example 58 with AMQPMessage

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

the class TransactionController method discharge.

private synchronized void discharge(TxnIdIF txnId, boolean fail) throws AMQPException {
    AMQPMessage msg = new AMQPMessage();
    Discharge discharge = new Discharge();
    discharge.setTxnId(txnId);
    discharge.setFail(new AMQPBoolean(fail));
    msg.setAmqpValue(new AmqpValue(discharge));
    DeliveryStateIF deliveryState = producer.send(msg);
    if (deliveryState instanceof Rejected) {
        Rejected rejected = (Rejected) deliveryState;
        com.swiftmq.amqp.v100.generated.transport.definitions.Error error = rejected.getError();
        if (error != null)
            throw new AMQPException(error.getValueString());
        else
            throw new AMQPException(("Unknown transactiom error"));
    }
}
Also used : DeliveryStateIF(com.swiftmq.amqp.v100.generated.messaging.delivery_state.DeliveryStateIF) Rejected(com.swiftmq.amqp.v100.generated.messaging.delivery_state.Rejected) AMQPBoolean(com.swiftmq.amqp.v100.types.AMQPBoolean) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)

Example 59 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 {
        boolean rollback = false;
        int i = 0;
        while (i < nMsgs) {
            TxnIdIF txnIdIF = txc.createTxnId();
            consumer.acquire(1, txnIdIF);
            AMQPMessage request = consumer.receive();
            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);
            if (rollback)
                txc.rollback(txnIdIF);
            else {
                txc.commit(txnIdIF);
                i++;
            }
            rollback = !rollback;
        }
    } 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)

Example 60 with AMQPMessage

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

the class BodyTypeMessageFactory method create.

public MessageImpl create(AMQPMessage source) throws Exception {
    List data = source.getData();
    if (data != null && data.size() > 0) {
        if (data.size() == 1) {
            byte[] b = ((Data) data.get(0)).getValue();
            return new BytesMessageImpl(b, b.length);
        } else {
            BytesMessageImpl msg = new BytesMessageImpl();
            for (int i = 0; i < data.size(); i++) {
                msg.writeBytes(((Data) data.get(i)).getValue());
            }
            return msg;
        }
    }
    List sequenceList = source.getAmqpSequence();
    if (sequenceList != null && sequenceList.size() > 0) {
        StreamMessageImpl msg = new StreamMessageImpl();
        for (int i = 0; i < sequenceList.size(); i++) {
            AmqpSequence seq = (AmqpSequence) sequenceList.get(i);
            List list = seq.getValue();
            if (list != null && list.size() > 0) {
                for (int j = 0; j < list.size(); j++) {
                    try {
                        msg.writeObject(Util.convertAMQPtoJMS((AMQPType) list.get(j)));
                    } catch (Exception e) {
                        msg.setBooleanProperty(PROP_ERROR, true);
                        msg.setStringProperty(PROP_ERROR_CAUSE, e.toString());
                        break;
                    }
                }
            }
        }
        return msg;
    }
    AmqpValue amqpValue = source.getAmqpValue();
    if (amqpValue != null) {
        AMQPType value = amqpValue.getValue();
        AMQPDescribedConstructor constructor = value.getConstructor();
        int code = constructor != null ? constructor.getFormatCode() : value.getCode();
        if (AMQPTypeDecoder.isSymbol(code) || AMQPTypeDecoder.isString(code)) {
            TextMessageImpl msg = new TextMessageImpl();
            msg.setText((String) Util.convertAMQPtoJMS(value));
            return msg;
        }
        if (AMQPTypeDecoder.isBinary(code)) {
            byte[] b = ((AMQPBinary) value).getValue();
            return new BytesMessageImpl(b, b.length);
        }
        if (AMQPTypeDecoder.isList(code)) {
            StreamMessageImpl msg = new StreamMessageImpl();
            List list = ((AMQPList) value).getValue();
            for (int i = 0; i < list.size(); i++) {
                try {
                    msg.writeObject(Util.convertAMQPtoJMS((AMQPType) list.get(i)));
                } catch (Exception e) {
                    msg.setBooleanProperty(PROP_ERROR, true);
                    msg.setStringProperty(PROP_ERROR_CAUSE, e.toString());
                    break;
                }
            }
            return msg;
        }
        if (AMQPTypeDecoder.isMap(code)) {
            MapMessageImpl msg = new MapMessageImpl();
            Map map = ((AMQPMap) value).getValue();
            for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
                Map.Entry entry = (Map.Entry) iter.next();
                try {
                    String name = Util.convertAMQPtoJMS((AMQPType) entry.getKey()).toString();
                    msg.setObject(name, Util.convertAMQPtoJMS((AMQPType) entry.getValue()));
                } catch (Exception e) {
                    msg.setBooleanProperty(PROP_ERROR, true);
                    msg.setStringProperty(PROP_ERROR_CAUSE, e.toString());
                    break;
                }
            }
            return msg;
        }
        // Everything else is a ObjectMessage
        ObjectMessageImpl msg = new ObjectMessageImpl();
        try {
            msg.setObject((Serializable) Util.convertAMQPtoJMS(value));
        } catch (Exception e) {
            msg.setBooleanProperty(PROP_ERROR, true);
            msg.setStringProperty(PROP_ERROR_CAUSE, e.toString());
        }
        return msg;
    }
    return new MessageImpl();
}
Also used : Data(com.swiftmq.amqp.v100.generated.messaging.message_format.Data) AmqpSequence(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpSequence) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue) Iterator(java.util.Iterator) List(java.util.List) Map(java.util.Map)

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