Search in sources :

Example 1 with DeliveryTag

use of com.swiftmq.amqp.v100.generated.transport.definitions.DeliveryTag in project swiftmq-ce by iitsoftware.

the class SourceLink method verifyLocalAddress.

public void verifyLocalAddress() throws AuthenticationException, QueueException, TopicException, InvalidSelectorException {
    if (!dynamic) {
        // This is for the case of reconnecting to a Durable Subscriber without specifying a Source in the attach frame.
        if (localAddress == null) {
            String topicName = ctx.topicManager.getDurableTopicName(getName(), mySessionHandler.getVersionedConnection().getActiveLogin());
            if (topicName != null)
                localAddress = new AddressString(topicName);
            durability = TerminusDurability.CONFIGURATION;
        }
        super.verifyLocalAddress();
    }
    if (receiver == null) {
        MessageSelector msel = null;
        if (messageSelector != null) {
            msel = new MessageSelector(messageSelector);
            msel.compile();
        }
        if (dynamic) {
            expiryPolicy = TerminusExpiryPolicy.LINK_DETACH;
            durability = TerminusDurability.NONE;
            // sndSettleMode = SenderSettleMode.SETTLED.getValue();       // SETTLED is only possible with temp queues because bulk mode in message proc do not delete from persistent store
            queueName = ctx.queueManager.createTemporaryQueue();
            receiver = ctx.queueManager.createQueueReceiver(queueName, mySessionHandler.getVersionedConnection().getActiveLogin(), msel);
        } else {
            if (isQueue) {
                expiryPolicy = TerminusExpiryPolicy.LINK_DETACH;
                durability = TerminusDurability.CONFIGURATION;
                // sndSettleMode = SenderSettleMode.UNSETTLED.getValue();
                receiver = ctx.queueManager.createQueueReceiver(getLocalAddress().getValueString(), mySessionHandler.getVersionedConnection().getActiveLogin(), msel);
            } else {
                if (durability.getValue() == TerminusDurability.CONFIGURATION.getValue() || durability.getValue() == TerminusDurability.UNSETTLED_STATE.getValue()) {
                    if (!expiryPolicy.getValue().equals(TerminusExpiryPolicy.LINK_DETACH.getValue()))
                        expiryPolicy = TerminusExpiryPolicy.NEVER;
                    durability = TerminusDurability.CONFIGURATION;
                    // sndSettleMode = SenderSettleMode.UNSETTLED.getValue();
                    queueName = ctx.topicManager.subscribeDurable(name, (TopicImpl) getLocalDestination(), msel, noLocal, mySessionHandler.getVersionedConnection().getActiveLogin());
                } else {
                    expiryPolicy = TerminusExpiryPolicy.LINK_DETACH;
                    queueName = ctx.queueManager.createTemporaryQueue();
                    // sndSettleMode = SenderSettleMode.SETTLED.getValue(); // SETTLED is only possible with temp queues because bulk mode in message proc do not delete from persistent store
                    subscriberId = ctx.topicManager.subscribe((TopicImpl) localDestination, msel, noLocal, queueName, mySessionHandler.getVersionedConnection().getActiveLogin());
                }
                receiver = ctx.queueManager.createQueueReceiver(queueName, mySessionHandler.getVersionedConnection().getActiveLogin(), null);
            }
        }
    }
    if (remoteUnsettled != null) {
        final AbstractQueue aq = ctx.queueManager.getQueueForInternalUse(receiver.getQueueName());
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/recovery, abstractQueue=" + aq);
        DataByteArrayInputStream dbis = new DataByteArrayInputStream();
        for (Iterator iter = remoteUnsettled.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iter.next();
            AMQPBinary deliveryTag = (AMQPBinary) entry.getKey();
            DeliveryStateIF deliveryStateIF = null;
            try {
                deliveryStateIF = DeliveryStateFactory.create((AMQPList) entry.getValue());
            } catch (Exception e) {
                throw new QueueException("Unable to create delivery tag");
            }
            final MessageIndex messageIndex = new MessageIndex();
            dbis.setBuffer(deliveryTag.getValue());
            try {
                messageIndex.readContent(dbis);
            } catch (IOException e) {
                throw new QueueException("Unable to convert delivery tag into a message index");
            }
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/recovery, messageIndex=" + messageIndex + ", deliveryStateIF=" + deliveryStateIF);
            if (deliveryStateIF != null) {
                deliveryStateIF.accept(new DeliveryStateVisitorAdapter() {

                    public void visit(Accepted accepted) {
                        try {
                            if (aq != null) {
                                MessageIndex indexEntry = aq.getIndexEntry(messageIndex);
                                if (indexEntry != null) {
                                    aq.removeMessageByIndex(indexEntry);
                                    if (ctx.traceSpace.enabled)
                                        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/recovery, messageIndex=" + indexEntry + ", removed");
                                } else {
                                    if (ctx.traceSpace.enabled)
                                        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/recovery, messageIndex=" + messageIndex + ", NOT FOUND!");
                                }
                            }
                        } catch (QueueException e) {
                            e.printStackTrace();
                            if (ctx.traceSpace.enabled)
                                ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/recovery, messageIndex=" + messageIndex + ", exception=" + e);
                        }
                    }
                });
            }
        }
    }
}
Also used : AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) IOException(java.io.IOException) DataByteArrayInputStream(com.swiftmq.tools.util.DataByteArrayInputStream) IOException(java.io.IOException) AuthenticationException(com.swiftmq.swiftlet.auth.AuthenticationException) InvalidSelectorException(javax.jms.InvalidSelectorException) TopicException(com.swiftmq.swiftlet.topic.TopicException) Iterator(java.util.Iterator) MessageSelector(com.swiftmq.ms.MessageSelector) TopicImpl(com.swiftmq.jms.TopicImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with DeliveryTag

use of com.swiftmq.amqp.v100.generated.transport.definitions.DeliveryTag in project swiftmq-ce by iitsoftware.

the class TargetLink method transformAndStore.

private void transformAndStore(TransferFrame frame) throws EndWithErrorException {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/transformAndStore, frame=" + frame);
    if (mySessionHandler.maxMessageSize > 0 && frame.getPayloadLength() > mySessionHandler.maxMessageSize)
        throw new LinkEndException(this, LinkError.MESSAGE_SIZE_EXCEEDED, new AMQPString("Message size (" + frame.getPayloadLength() + ") > max message size (" + mySessionHandler.maxMessageSize));
    if (coordinator)
        handleTransactionRequest(frame);
    else {
        try {
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/transformAndStore, localDestination=" + localDestination);
            long payloadLength = frame.getPayloadLength();
            MessageImpl msg = getTransformer(frame.getMessageFormat().getValue()).transform(frame, this);
            if (msg.getJMSDestination() == null)
                msg.setJMSDestination(getLocalDestination());
            msg.setStringProperty(MessageImpl.PROP_CLIENT_ID, mySessionHandler.getVersionedConnection().getActiveLogin().getClientId());
            if (remoteUnsettled != null) {
                DeliveryTag deliveryTag = frame.getDeliveryTag();
                if (remoteUnsettled.remove(deliveryTag) != null) {
                    msg.setBooleanProperty(MessageImpl.PROP_DOUBT_DUPLICATE, true);
                    if (remoteUnsettled.size() == 0)
                        remoteUnsettled = null;
                }
            }
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + "/transformAndStore, msg=" + msg);
            DeliveryStateIF state = frame.getState();
            if (state != null && state instanceof TransactionalState) {
                TransactionalState txState = (TransactionalState) state;
                TxnIdIF txnId = txState.getTxnId();
                if (txnId != null) {
                    transactionRegistry.addToTransaction(txnId, name, msg, this);
                } else
                    throw new SessionEndException(TransactionError.UNKNOWN_ID, new AMQPString("Missing TxnId in TransactionalState: " + txState));
            } else {
                QueuePushTransaction tx = sender.createTransaction();
                tx.putMessage(msg);
                tx.commit();
                setFlowcontrolDelay(tx.getFlowControlDelay());
            }
        } catch (Exception e) {
            throw new SessionEndException(AmqpError.INTERNAL_ERROR, new AMQPString("Exception during transformAndStore: " + e));
        }
    }
}
Also used : QueuePushTransaction(com.swiftmq.swiftlet.queue.QueuePushTransaction) DeliveryStateIF(com.swiftmq.amqp.v100.generated.messaging.delivery_state.DeliveryStateIF) MessageImpl(com.swiftmq.jms.MessageImpl) 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 3 with DeliveryTag

use of com.swiftmq.amqp.v100.generated.transport.definitions.DeliveryTag in project swiftmq-client by iitsoftware.

the class SessionDispatcher method doSend.

private void doSend(POSendMessage po) {
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", doSend, po=" + po + ", dataLength=" + po.getPackager().getSize());
    try {
        Producer producer = po.getProducer();
        producer.verifyState();
        Packager packager = po.getPackager();
        if (remoteIncomingWindow > 0 && outgoingWindow > 0) {
            do {
                boolean wasFirstPacket = false;
                boolean isAtMostOnce = producer.getQoS() == QoS.AT_MOST_ONCE;
                packager.setMaxFrameSize(mySession.myConnection.connectionDispatcher.getMaxFrameSize());
                TransferFrame frame = new TransferFrame(mySession.getChannel());
                frame.setHandle(new Handle(producer.getHandle()));
                frame.setSettled(new AMQPBoolean(isAtMostOnce));
                if (packager.getCurrentPacketNumber() == 0) {
                    long dId = nextDeliveryId();
                    wasFirstPacket = true;
                    producer.incDeliveryCountSnd();
                    DeliveryTag deliveryTag = po.getDeliveryTag() != null ? po.getDeliveryTag() : producer.createDeliveryTag();
                    if (!isAtMostOnce) {
                        if (po.getTxnId() == null && !po.isRecovery())
                            producer.getDeliveryMemory().addUnsettledDelivery(new UnsettledDelivery(deliveryTag, null, po.getMessage()));
                        unsettledOutgoingDeliveries.put(dId, new DeliveryMapping(deliveryTag, producer));
                    }
                    frame.setDeliveryTag(deliveryTag);
                    frame.setDeliveryId(new DeliveryNumber(dId));
                    frame.setMessageFormat(new MessageFormat(0));
                    TxnIdIF txnId = po.getTxnId();
                    if (txnId != null) {
                        TransactionalState txState = new TransactionalState();
                        txState.setTxnId(txnId);
                        frame.setState(txState);
                    }
                }
                packager.setMessageFormat(0);
                packager.getNextPacket(frame);
                // We may increase the outgoing window and send a flow before
                if (wasFirstPacket && outgoingWindow - packager.getPredictedNumberPackets() < 0) {
                    outgoingWindow += packager.getPredictedNumberPackets();
                    sendFlow();
                    windowChanged = true;
                }
                if (pTracer.isEnabled())
                    pTracer.trace(toString(), ", doSend, remoteIncomingWindows=" + remoteIncomingWindow + ", outgoingWindow=" + outgoingWindow + ", sending message, wasFirstPacket=" + wasFirstPacket + ", maxSize=" + packager.getMaxPayloadLength() + ", packetSize=" + frame.getPayload().length + ", predictedNumberPackets=" + packager.getPredictedNumberPackets() + ", currentPacket=" + packager.getCurrentPacketNumber() + ", hasMore=" + packager.hasMore());
                outboundHandler.send(frame);
                nextOutgoingId++;
                remoteIncomingWindow--;
                if (!isAtMostOnce)
                    outgoingWindow--;
                if (!packager.hasMore()) {
                    // b) everything else: after the last packet has been sent
                    if (producer.isTransactionController() || po.getTxnId() != null)
                        producer.setWaitingPO(po);
                    else {
                        po.setSuccess(true);
                        if (po.getSemaphore() != null)
                            po.getSemaphore().notifySingleWaiter();
                    }
                    // If that was the last packet and outgoing window was increased for this message, we need to reset it and send another flow
                    if (windowChanged) {
                        outgoingWindow = mySession.getOutgoingWindowSize();
                        sendFlow();
                    }
                    break;
                }
            } while (remoteIncomingWindow > 0 && outgoingWindow > 0);
            if (packager.hasMore()) {
                if (pTracer.isEnabled())
                    pTracer.trace(toString(), ", doSend, remoteIncomingWindows=" + remoteIncomingWindow + ", outgoingWindow=" + outgoingWindow + ", has more but no window, storing message");
                outboundDeliveries.add(po);
            }
        } else {
            if (pTracer.isEnabled())
                pTracer.trace(toString(), ", doSend, po=" + po + ", remoteIncomingWindows=" + remoteIncomingWindow + ", outgoingWindow=" + outgoingWindow + ", no window, storing message");
            outboundDeliveries.add(po);
        }
    } catch (Exception e) {
        po.setSuccess(false);
        po.setException(e.getMessage());
        if (po.getSemaphore() != null)
            po.getSemaphore().notifySingleWaiter();
    }
}
Also used : TxnIdIF(com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF) IOException(java.io.IOException) TransactionalState(com.swiftmq.amqp.v100.generated.transactions.coordination.TransactionalState) Packager(com.swiftmq.amqp.v100.transport.Packager)

Example 4 with DeliveryTag

use of com.swiftmq.amqp.v100.generated.transport.definitions.DeliveryTag in project swiftmq-client by iitsoftware.

the class Producer method recover.

protected void recover(AMQPMap remoteUnsettled) {
    try {
        if (remoteUnsettled != null) {
            Map<AMQPType, AMQPType> map = remoteUnsettled.getValue();
            for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
                Map.Entry entry = (Map.Entry) iter.next();
                final DeliveryTag deliveryTag = new DeliveryTag(((AMQPBinary) entry.getKey()).getValue());
                final AMQPList deliveryState = (AMQPList) entry.getValue();
                if (deliveryState != null) {
                    try {
                        DeliveryStateFactory.create(deliveryState).accept(new DeliveryStateVisitorAdapter() {

                            public void visit(Accepted impl) {
                                deliveryMemory.deliverySettled(deliveryTag);
                                mySession.dispatch(new POSendResumedTransfer(Producer.this, deliveryTag));
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if (deliveryMemory.getNumberUnsettled() > 0) {
            Collection<UnsettledDelivery> unsettled = deliveryMemory.getUnsettled();
            for (Iterator<UnsettledDelivery> iter = unsettled.iterator(); iter.hasNext(); ) {
                UnsettledDelivery unsettledDelivery = iter.next();
                if (unsettledDelivery.getMessage() != null) {
                    AMQPMessage msg = unsettledDelivery.getMessage();
                    if (msg.getTxnIdIF() == null) {
                        POSendMessage po = new POSendMessage(null, this, msg, null, unsettledDelivery.getDeliveryTag());
                        po.setRecovery(true);
                        mySession.dispatch(po);
                    }
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : DeliveryStateVisitorAdapter(com.swiftmq.amqp.v100.generated.messaging.delivery_state.DeliveryStateVisitorAdapter) POSendResumedTransfer(com.swiftmq.amqp.v100.client.po.POSendResumedTransfer) POSendMessage(com.swiftmq.amqp.v100.client.po.POSendMessage) IOException(java.io.IOException) DeliveryTag(com.swiftmq.amqp.v100.generated.transport.definitions.DeliveryTag) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) Accepted(com.swiftmq.amqp.v100.generated.messaging.delivery_state.Accepted) IOException(java.io.IOException) Iterator(java.util.Iterator) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)4 AuthenticationException (com.swiftmq.swiftlet.auth.AuthenticationException)2 TopicException (com.swiftmq.swiftlet.topic.TopicException)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 InvalidSelectorException (javax.jms.InvalidSelectorException)2 POSendMessage (com.swiftmq.amqp.v100.client.po.POSendMessage)1 POSendResumedTransfer (com.swiftmq.amqp.v100.client.po.POSendResumedTransfer)1 Accepted (com.swiftmq.amqp.v100.generated.messaging.delivery_state.Accepted)1 DeliveryStateIF (com.swiftmq.amqp.v100.generated.messaging.delivery_state.DeliveryStateIF)1 DeliveryStateVisitorAdapter (com.swiftmq.amqp.v100.generated.messaging.delivery_state.DeliveryStateVisitorAdapter)1 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)1 TransactionalState (com.swiftmq.amqp.v100.generated.transactions.coordination.TransactionalState)1 TxnIdIF (com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF)1 DeliveryTag (com.swiftmq.amqp.v100.generated.transport.definitions.DeliveryTag)1 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)1 Packager (com.swiftmq.amqp.v100.transport.Packager)1 MessageImpl (com.swiftmq.jms.MessageImpl)1 TopicImpl (com.swiftmq.jms.TopicImpl)1 MessageSelector (com.swiftmq.ms.MessageSelector)1