Search in sources :

Example 1 with AMQPException

use of com.swiftmq.amqp.v100.client.AMQPException in project swiftmq-ce by iitsoftware.

the class AMQPNativeInboundTransformer method transform.

public MessageImpl transform(TransferFrame frame, DestinationFactory destinationFactory) throws AMQPException, JMSException {
    BytesMessageImpl msg = null;
    if (frame.getMorePayloads() == null) {
        byte[] payload = frame.getPayload();
        msg = new BytesMessageImpl(payload, payload.length);
    } else {
        byte[] b = new byte[frame.getPayloadLength()];
        byte[] payload = frame.getPayload();
        System.arraycopy(payload, 0, b, 0, payload.length);
        int pos = payload.length;
        List morePayloads = frame.getMorePayloads();
        for (int i = 0; i < morePayloads.size(); i++) {
            payload = (byte[]) morePayloads.get(i);
            System.arraycopy(payload, 0, b, pos, payload.length);
            pos += payload.length;
        }
        msg = new BytesMessageImpl(b, b.length);
    }
    try {
        msg.setLongProperty(messageFormat, frame.getMessageFormat().getValue());
        msg.setBooleanProperty(amqpNative, true);
        setDefaultHeader(msg);
    } catch (Exception e) {
        throw new AMQPException(e.toString());
    }
    msg.reset();
    return msg;
}
Also used : AMQPException(com.swiftmq.amqp.v100.client.AMQPException) BytesMessageImpl(com.swiftmq.jms.BytesMessageImpl) List(java.util.List) AMQPException(com.swiftmq.amqp.v100.client.AMQPException) JMSException(javax.jms.JMSException)

Example 2 with AMQPException

use of com.swiftmq.amqp.v100.client.AMQPException 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 3 with AMQPException

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

the class TransactionController method createTxnId.

/**
 * Creates a new transaction id.
 *
 * @return transaction id
 * @throws AMQPException on error
 */
public synchronized TxnIdIF createTxnId() throws AMQPException {
    if (producer == null) {
        producer = mySession.createProducer(Coordinator.DESCRIPTOR_NAME, QoS.AT_LEAST_ONCE);
        producer.setTransactionController(true);
        Set capa = producer.getDestinationCapabilities();
        if (capa != null) {
            if (capa.contains(TxnCapability.LOCAL_TRANSACTIONS.getValue()))
                supportLocalTransactions = true;
            supportDistributedTransactions = capa.contains(TxnCapability.DISTRIBUTED_TRANSACTIONS.getValue());
            supportPromotableTransactions = capa.contains(TxnCapability.PROMOTABLE_TRANSACTIONS.getValue());
            supportMultiTxnsPerSsn = capa.contains(TxnCapability.MULTI_TXNS_PER_SSN.getValue());
            supportMultiSsnsPerTxn = capa.contains(TxnCapability.MULTI_SSNS_PER_TXN.getValue());
        }
    }
    AMQPMessage msg = new AMQPMessage();
    msg.setAmqpValue(new AmqpValue(new Declare()));
    Declared declared = (Declared) producer.send(msg);
    return declared == null ? null : declared.getTxnId();
}
Also used : Set(java.util.Set) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)

Example 4 with AMQPException

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

the class DurableConsumer method unsubscribe.

/**
 * Unsubscribes the durable consumer and destroys the durable link.
 *
 * @throws AMQPException
 */
public void unsubscribe() throws AMQPException {
    if (!closed)
        close();
    Semaphore sem = new Semaphore();
    POAttachDurableConsumer po = new POAttachDurableConsumer(sem, name, source, linkCredit, qoS, false, null, TerminusExpiryPolicy.LINK_DETACH, deliveryMemory);
    mySession.getSessionDispatcher().dispatch(po);
    sem.waitHere();
    if (!po.isSuccess())
        throw new AMQPException(po.getException());
    DurableConsumer c = (DurableConsumer) po.getLink();
    c.close();
}
Also used : POAttachDurableConsumer(com.swiftmq.amqp.v100.client.po.POAttachDurableConsumer) Semaphore(com.swiftmq.tools.concurrent.Semaphore) POAttachDurableConsumer(com.swiftmq.amqp.v100.client.po.POAttachDurableConsumer)

Example 5 with AMQPException

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

the class Producer method send.

/**
 * Send a message to the target. For transactional sends the method returns after settlement has been finished, otherwise when the message has been sent.
 *
 * @param msg        message
 * @param persistent whether the message should send/stored durable
 * @param priority   message priority (default is 5)
 * @param ttl        time to live (expiration) in milliseconds, default no expiration
 * @return delivery state of the message
 * @throws AMQPException on error
 */
public DeliveryStateIF send(AMQPMessage msg, boolean persistent, int priority, long ttl) throws AMQPException {
    verifyState();
    Header header = msg.getHeader();
    if (header == null) {
        header = new Header();
        msg.setHeader(header);
    }
    header.setDurable(new AMQPBoolean(persistent));
    header.setPriority(new AMQPUnsignedByte(priority));
    if (ttl >= 0)
        header.setTtl(new Milliseconds(ttl));
    Properties props = msg.getProperties();
    if (props == null) {
        props = new Properties();
        msg.setProperties(props);
    }
    if (props.getMessageId() == null)
        props.setMessageId(new MessageIdString(nextMsgId()));
    props.setTo(new AddressString(target));
    String userName = mySession.myConnection.getUserName();
    if (userName != null)
        props.setUserId(new AMQPBinary(userName.getBytes()));
    Semaphore sem = new Semaphore();
    try {
        POSendMessage po = new POSendMessage(sem, this, msg, msg.getTxnIdIF(), msg.getDeliveryTag());
        mySession.dispatch(po);
        sem.waitHere();
        if (!po.isSuccess())
            throw new AMQPException(po.getException());
        return po.getDeliveryState();
    } catch (Exception e) {
        e.printStackTrace();
        throw new AMQPException(e.toString());
    }
}
Also used : AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) POSendMessage(com.swiftmq.amqp.v100.client.po.POSendMessage) MessageIdString(com.swiftmq.amqp.v100.generated.messaging.message_format.MessageIdString) MessageIdString(com.swiftmq.amqp.v100.generated.messaging.message_format.MessageIdString) AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) Semaphore(com.swiftmq.tools.concurrent.Semaphore) Properties(com.swiftmq.amqp.v100.generated.messaging.message_format.Properties) Milliseconds(com.swiftmq.amqp.v100.generated.transport.definitions.Milliseconds) IOException(java.io.IOException) Header(com.swiftmq.amqp.v100.generated.messaging.message_format.Header)

Aggregations

AMQPException (com.swiftmq.amqp.v100.client.AMQPException)4 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)4 AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)3 Semaphore (com.swiftmq.tools.concurrent.Semaphore)3 IOException (java.io.IOException)3 JMSException (javax.jms.JMSException)3 Milliseconds (com.swiftmq.amqp.v100.generated.transport.definitions.Milliseconds)2 MessageImpl (com.swiftmq.jms.MessageImpl)2 List (java.util.List)2 POAttachDurableConsumer (com.swiftmq.amqp.v100.client.po.POAttachDurableConsumer)1 POCloseLink (com.swiftmq.amqp.v100.client.po.POCloseLink)1 POSendMessage (com.swiftmq.amqp.v100.client.po.POSendMessage)1 DeliveryStateIF (com.swiftmq.amqp.v100.generated.messaging.delivery_state.DeliveryStateIF)1 Rejected (com.swiftmq.amqp.v100.generated.messaging.delivery_state.Rejected)1 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)1 AmqpSequence (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpSequence)1 Data (com.swiftmq.amqp.v100.generated.messaging.message_format.Data)1 Header (com.swiftmq.amqp.v100.generated.messaging.message_format.Header)1 MessageIdString (com.swiftmq.amqp.v100.generated.messaging.message_format.MessageIdString)1 Properties (com.swiftmq.amqp.v100.generated.messaging.message_format.Properties)1