Search in sources :

Example 26 with MessageImpl

use of com.swiftmq.jms.MessageImpl in project swiftmq-ce by iitsoftware.

the class NonXADeliveryStage method processTransactionRequest.

private long processTransactionRequest(NonXATransactionRequest request) throws Exception {
    long delay = 0;
    int txNo = request.getSequenceNo();
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, txNo=" + txNo + " ...");
    Tx tx = new Tx(txNo);
    List messageList = request.getMessageList();
    for (int i = 0; i < messageList.size(); i++) {
        boolean msgValid = true;
        MessageImpl msg = (MessageImpl) messageList.get(i);
        String queueName = null;
        if (msg.getDestRouter().equals(ctx.routerName))
            queueName = msg.getDestQueue();
        else
            queueName = SchedulerRegistry.QUEUE_PREFIX + msg.getDestRouter();
        QueueSender sender = (QueueSender) producers.get(queueName);
        if (sender == null) {
            try {
                sender = ctx.queueManager.createQueueSender(queueName, null);
                producers.put(queueName, sender);
            } catch (Exception e) {
                if (ctx.traceSpace.enabled)
                    ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, txNo=" + txNo + ", exception creating sender, queue=" + queueName);
                if (ctx.queueManager.isTemporaryQueue(queueName)) {
                    if (ctx.traceSpace.enabled)
                        ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, txNo=" + txNo + ", temp queue, forget it");
                    msgValid = false;
                } else {
                    if (ctx.traceSpace.enabled)
                        ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, txNo=" + txNo + ", using unroutable queue");
                    sender = (QueueSender) producers.get(RoutingSwiftletImpl.UNROUTABLE_QUEUE);
                    msg.setStringProperty(MessageImpl.PROP_UNROUTABLE_REASON, e.toString());
                }
            }
        }
        if (msgValid) {
            try {
                // Check redelivered and activate duplicate message detection for this message by setting the DOUBT property
                if (msg.getJMSRedelivered()) {
                    if (ctx.traceSpace.enabled)
                        ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, txNo=" + txNo + ", message is redelivered, activating dups detection for it");
                    msg.setJMSRedelivered(false);
                    msg.setBooleanProperty(MessageImpl.PROP_DOUBT_DUPLICATE, true);
                }
                QueuePushTransaction t = tx.getTransaction(queueName);
                if (t == null) {
                    t = sender.createTransaction();
                    tx.addTransaction(queueName, t);
                }
                t.putMessage(msg);
            } catch (Exception e) {
                try {
                    if (!sender.getQueueName().startsWith(RoutingSwiftletImpl.UNROUTABLE_QUEUE))
                        sender.close();
                } catch (Exception e1) {
                }
                producers.remove(queueName);
                if (ctx.traceSpace.enabled)
                    ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, txNo=" + txNo + ", exception put message, queue=" + queueName);
                if (ctx.queueManager.isTemporaryQueue(queueName)) {
                    if (ctx.traceSpace.enabled)
                        ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, txNo=" + txNo + ", temp queue, forget it");
                } else {
                    if (ctx.traceSpace.enabled)
                        ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, txNo=" + txNo + ", using unroutable queue");
                    sender = (QueueSender) producers.get(RoutingSwiftletImpl.UNROUTABLE_QUEUE);
                    msg.setStringProperty(MessageImpl.PROP_UNROUTABLE_REASON, e.toString());
                    QueuePushTransaction t = tx.getTransaction(RoutingSwiftletImpl.UNROUTABLE_QUEUE);
                    if (t == null) {
                        t = sender.createTransaction();
                        tx.addTransaction(RoutingSwiftletImpl.UNROUTABLE_QUEUE, t);
                    }
                    t.putMessage(msg);
                }
            }
        }
    }
    try {
        delay = tx.commit();
    } catch (Exception e) {
        tx.rollback();
        throw e;
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, txNo=" + txNo + " done");
    return delay;
}
Also used : MessageImpl(com.swiftmq.jms.MessageImpl) JMSException(javax.jms.JMSException)

Example 27 with MessageImpl

use of com.swiftmq.jms.MessageImpl in project swiftmq-client by iitsoftware.

the class MessageConsumerImpl method addToCache.

synchronized void addToCache(AsyncMessageDeliveryRequest request) {
    if (isClosed())
        return;
    if (request.isRequiresRestart())
        fillCachePending = false;
    MessageImpl msg = request.getMessageEntry().getMessage();
    if (request.getConnectionId() != mySession.myConnection.getConnectionId()) {
        if (MessageTracker.enabled) {
            MessageTracker.getInstance().track(((AsyncMessageDeliveryRequest) request).getMessageEntry().getMessage(), new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "addToCache, invalid connectionId (" + request.getConnectionId() + " vs " + mySession.myConnection.getConnectionId() + ")");
        }
        return;
    }
    if (MessageTracker.enabled) {
        MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "addToCache");
    }
    messageCache.add(request);
}
Also used : MessageImpl(com.swiftmq.jms.MessageImpl)

Example 28 with MessageImpl

use of com.swiftmq.jms.MessageImpl in project swiftmq-client by iitsoftware.

the class MessageConsumerImpl method receiveMessage.

synchronized Message receiveMessage(boolean block, long timeout) throws JMSException {
    verifyState();
    if (messageListener != null) {
        throw new JMSException("receive not allowed while a message listener has been set");
    }
    boolean wasDuplicate = false;
    boolean wasInvalidConnectionId = false;
    MessageImpl msg = null;
    String id = null;
    long to = timeout;
    do {
        wasDuplicate = false;
        wasInvalidConnectionId = false;
        if (!consumerStarted)
            fillCache();
        do {
            if (messageCache.getSize() == 0) {
                if (block) {
                    receiverWaiting = true;
                    if (timeout == 0)
                        UninterruptableWaiter.doWait(this);
                    else {
                        long startWait = System.currentTimeMillis();
                        UninterruptableWaiter.doWait(this, to);
                        to -= System.currentTimeMillis() - startWait;
                        if (to <= 0)
                            return null;
                    }
                } else {
                    if (fillCachePending && receiveNoWaitFirstCall) {
                        UninterruptableWaiter.doWait(this, 1000);
                    }
                }
            }
        } while (mySession.resetInProgress);
        receiverWaiting = false;
        if (messageCache.getSize() == 0 || isClosed())
            return null;
        AsyncMessageDeliveryRequest request = (AsyncMessageDeliveryRequest) messageCache.remove();
        if (request.getConnectionId() != mySession.myConnection.getConnectionId()) {
            if (MessageTracker.enabled) {
                MessageTracker.getInstance().track(request.getMessageEntry().getMessage(), new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "receiveMessage, invalid connectionId (" + request.getConnectionId() + " vs " + mySession.myConnection.getConnectionId() + ")");
            }
            wasInvalidConnectionId = true;
        } else {
            MessageEntry messageEntry = request.getMessageEntry();
            msg = messageEntry.getMessage();
            messageEntry.moveMessageAttributes();
            msg.setMessageConsumerImpl(this);
            msg.reset();
            msg.setReadOnly(true);
            msg.setUseThreadContextCL(useThreadContextCL);
            if (request.isRequiresRestart())
                fillCache();
            if (recordLog) {
                id = SessionImpl.buildId(uniqueConsumerId, msg);
                wasDuplicate = mySession.myConnection.isDuplicateMessageDetection() && mySession.isDuplicate(id);
            }
            if (MessageTracker.enabled) {
                MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "receivedMessage, duplicate=" + wasDuplicate);
            }
            if (reportDelivered)
                reportDelivered(msg, false);
            if (doAck) {
                try {
                    if (MessageTracker.enabled) {
                        MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "receivedMessage, ack...");
                    }
                    boolean cancelled = acknowledgeMessage(msg.getMessageIndex(), false);
                    if (MessageTracker.enabled) {
                        MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "receivedMessage, ack, cancelled=" + cancelled);
                    }
                } catch (JMSException e) {
                }
            }
            if (wasDuplicate) {
                msg = null;
            }
        }
    } while (wasDuplicate || wasInvalidConnectionId);
    if (recordLog && mySession.myConnection.isDuplicateMessageDetection())
        mySession.addCurrentTxLog(id);
    return msg;
}
Also used : MessageEntry(com.swiftmq.swiftlet.queue.MessageEntry) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 29 with MessageImpl

use of com.swiftmq.jms.MessageImpl in project swiftmq-client by iitsoftware.

the class MessageConsumerImpl method reportDelivered.

private void reportDelivered(Message message, boolean duplicate) {
    try {
        MessageIndex messageIndex = ((MessageImpl) message).getMessageIndex();
        requestRegistry.request(new MessageDeliveredRequest(this, mySession.dispatchId, serverQueueConsumerId, messageIndex, duplicate));
    } catch (Exception e) {
    }
}
Also used : MessageIndex(com.swiftmq.swiftlet.queue.MessageIndex) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 30 with MessageImpl

use of com.swiftmq.jms.MessageImpl in project swiftmq-client by iitsoftware.

the class MessageConsumerImpl method invokeMessageListener.

private void invokeMessageListener() {
    if (isClosed())
        return;
    AsyncMessageDeliveryRequest request = (AsyncMessageDeliveryRequest) messageCache.remove();
    if (request.getConnectionId() != mySession.myConnection.getConnectionId()) {
        if (MessageTracker.enabled) {
            MessageTracker.getInstance().track(request.getMessageEntry().getMessage(), new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "invokeMessageListener, invalid connectionId (" + request.getConnectionId() + " vs " + mySession.myConnection.getConnectionId() + ")");
        }
        return;
    }
    MessageEntry messageEntry = request.getMessageEntry();
    MessageImpl msg = messageEntry.getMessage();
    messageEntry.moveMessageAttributes();
    MessageIndex msgIndex = msg.getMessageIndex();
    msg.setMessageConsumerImpl(this);
    try {
        msg.reset();
    } catch (JMSException e) {
        e.printStackTrace();
    }
    msg.setReadOnly(true);
    msg.setUseThreadContextCL(useThreadContextCL);
    String id = null;
    boolean duplicate = false;
    if (recordLog) {
        id = SessionImpl.buildId(uniqueConsumerId, msg);
        duplicate = mySession.myConnection.isDuplicateMessageDetection() && mySession.isDuplicate(id);
    }
    if (MessageTracker.enabled) {
        MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "invokeMessageListener, duplicate=" + duplicate);
    }
    if (reportDelivered)
        reportDelivered(msg, false);
    try {
        if (!duplicate) {
            if (recordLog && mySession.myConnection.isDuplicateMessageDetection())
                mySession.addCurrentTxLog(id);
            if (MessageTracker.enabled) {
                MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "invokeMessageListener, onMessage...");
            }
            mySession.withinOnMessage = true;
            mySession.setTxCancelled(false);
            messageListener.onMessage(msg);
            mySession.withinOnMessage = false;
            if (MessageTracker.enabled) {
                MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "invokeMessageListener, onMessage ok");
            }
            if (mySession.isTxCancelled() || mySession.acknowledgeMode == Session.CLIENT_ACKNOWLEDGE && msg.isCancelled()) {
                if (MessageTracker.enabled) {
                    MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "tx was cancelled, return!");
                }
                wasRecovered = false;
                return;
            }
        }
    } catch (RuntimeException e) {
        if (MessageTracker.enabled) {
            MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "invokeMessageListener, exception=" + e);
        }
        System.err.println("ERROR! MessageListener throws RuntimeException, shutting down consumer!");
        e.printStackTrace();
        try {
            close(e.toString());
        } catch (JMSException e1) {
        }
        return;
    }
    if (!wasRecovered) {
        if (request.isRequiresRestart())
            fillCache();
        if (doAck) {
            try {
                if (MessageTracker.enabled) {
                    MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "invokeMessageListener, ack");
                }
                boolean cancelled = acknowledgeMessage(msgIndex, false);
                if (MessageTracker.enabled) {
                    MessageTracker.getInstance().track(msg, new String[] { mySession.myConnection.toString(), mySession.toString(), toString() }, "invokeMessageListener, ack, cancelled=" + cancelled);
                }
            } catch (JMSException e) {
            }
        }
    } else
        wasRecovered = false;
}
Also used : MessageIndex(com.swiftmq.swiftlet.queue.MessageIndex) MessageEntry(com.swiftmq.swiftlet.queue.MessageEntry) MessageImpl(com.swiftmq.jms.MessageImpl)

Aggregations

MessageImpl (com.swiftmq.jms.MessageImpl)106 JMSException (javax.jms.JMSException)42 QueuePushTransaction (com.swiftmq.swiftlet.queue.QueuePushTransaction)33 InvalidSelectorException (javax.jms.InvalidSelectorException)31 ResourceLimitException (com.swiftmq.swiftlet.auth.ResourceLimitException)28 QueueImpl (com.swiftmq.jms.QueueImpl)21 DataByteArrayInputStream (com.swiftmq.tools.util.DataByteArrayInputStream)20 TopicImpl (com.swiftmq.jms.TopicImpl)17 MessageEntry (com.swiftmq.swiftlet.queue.MessageEntry)15 MessageIndex (com.swiftmq.swiftlet.queue.MessageIndex)15 RingBuffer (com.swiftmq.tools.collection.RingBuffer)14 InvalidDestinationException (javax.jms.InvalidDestinationException)14 List (java.util.List)13 EntityList (com.swiftmq.mgmt.EntityList)10 Message (javax.jms.Message)10 XidImpl (com.swiftmq.jms.XidImpl)6 DataByteArrayOutputStream (com.swiftmq.tools.util.DataByteArrayOutputStream)6 MessageSelector (com.swiftmq.ms.MessageSelector)5 XAContextException (com.swiftmq.swiftlet.xa.XAContextException)4 ArrayList (java.util.ArrayList)4