Search in sources :

Example 16 with Packet

use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.

the class MessageInfo method parseMessage.

/**
 * parse the message from the byte array.
 */
private Packet parseMessage(byte[] data) throws IOException {
    packetSize = data.length;
    ByteBuffer databuf = ByteBuffer.wrap(data);
    JMQByteBufferInputStream bis = new JMQByteBufferInputStream(databuf);
    try {
        Packet msg = new Packet(false);
        msg.generateTimestamp(false);
        msg.generateSequenceNumber(false);
        msg.readPacket(bis);
        return msg;
    } finally {
        bis.close();
    }
}
Also used : Packet(com.sun.messaging.jmq.io.Packet) JMQByteBufferInputStream(com.sun.messaging.jmq.io.JMQByteBufferInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 17 with Packet

use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.

the class SessionListener method process.

/**
 * method which handles delivering messages
 */
public void process() {
    if (sync) {
        // there doesnt need to be an external thread
        throw new RuntimeException("Cannot invoke SessionListener.process() when in synchronous receiving mode");
    }
    synchronized (sessionLock) {
        if (destroyed) {
            valid = false;
            sessionLock.notifyAll();
            return;
        }
        valid = true;
    }
    while (valid) {
        // if we are not busy, wait for the eventListener to wake us up
        while (valid && (!session.isBusy() || stopped)) {
            // get the lock
            synchronized (sessionLock) {
                islocked = true;
                sessionLock.notifyAll();
                // instead check the sessionLockNotify flag
                if (sessionLockNotify || !valid || stopped) {
                    sessionLockNotify = false;
                    continue;
                }
                try {
                    sessionLock.wait();
                } catch (Exception ex) {
                    Globals.getLogger().log(Logger.DEBUGHIGH, "Exception in sessionlock wait", ex);
                }
            }
        }
        if (!valid) {
            continue;
        }
        synchronized (sessionLock) {
            // we dont care if we are about to notify
            sessionLockNotify = false;
            islocked = false;
        }
        if (session.isBusy() && !stopped && valid) {
            // cool, we have something to do
            Packet p = new Packet();
            // retrieve the next packet and its ConsumerUID
            ConsumerUID uid = session.fillNextPacket(p);
            if (uid == null) {
                // weird, something went wrong, try again
                continue;
            }
            // Get the consumer object
            Consumer con = (Consumer) consumers.get(uid);
            try {
                JMSAck ack = null;
                // call the deliver method
                ack = con.deliver(p);
                if (ack != null) {
                    long transactionId = ack.getTransactionId(), consumerId = ack.getConsumerId();
                    SysMessageID sysMsgId = ack.getSysMessageID();
                    TransactionUID txnUID = null;
                    ConsumerUID conUID = null;
                    if (transactionId != 0) {
                        txnUID = new TransactionUID(transactionId);
                    }
                    if (consumerId != 0) {
                        conUID = new ConsumerUID(consumerId);
                    }
                    IMQConnection cxn = parent.checkConnectionId(ack.getConnectionId(), "Listener Thread");
                    SysMessageID[] ids = new SysMessageID[1];
                    // ids[0] = sysMsgId;
                    // ids[0] = ((Packet)p).getSysMessageID();
                    ids[0] = sysMsgId;
                    ConsumerUID[] cids = new ConsumerUID[1];
                    cids[0] = conUID;
                    Globals.getProtocol().acknowledge(cxn, txnUID, false, AckHandler.ACKNOWLEDGE_REQUEST, null, null, 0, ids, cids);
                }
            } catch (Exception ex) {
                if (ex instanceof ConsumerClosedNoDeliveryException) {
                    if (parent.getDEBUG()) {
                        Globals.getLogger().logStack(Logger.INFO, "DirectConsumer " + con + " is closed, message " + p.getSysMessageID() + " can not be deliverd", ex);
                    }
                } else {
                    // I have no idea what the exception might mean so just
                    // log it and go on
                    Globals.getLogger().logStack(Logger.ERROR, Globals.getBrokerResources().getKString(BrokerResources.X_CANNOT_DELIVER_MESSAGE_TO_CONSUMER, p.getSysMessageID(), uid + " DirectConsumer[" + con + "]"), ex);
                }
            }
        }
    }
}
Also used : Packet(com.sun.messaging.jmq.io.Packet) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) SysMessageID(com.sun.messaging.jmq.io.SysMessageID)

Example 18 with Packet

use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.

the class IMQDualThreadConnection method eventOccured.

@Override
public void eventOccured(EventType type, Reason r, Object target, Object oldval, Object newval, Object userdata) {
    // a session has something to do
    Session s = (Session) target;
    if (!runningMsgs) {
        // Connection is stopped so not sending messages to consumers
        return;
    }
    // Pull messages until not busy
    while (s.isBusy()) {
        // NOTE: this should work for queues because they require a resume flow from the client
        Packet emptyPacket = new Packet();
        s.fillNextPacket(emptyPacket);
        // write packet
        writePacket(emptyPacket, false);
    }
}
Also used : ReadWritePacket(com.sun.messaging.jmq.io.ReadWritePacket) Packet(com.sun.messaging.jmq.io.Packet) Session(com.sun.messaging.jmq.jmsserver.core.Session)

Example 19 with Packet

use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.

the class JMSWebSocket method processData.

private void processData(ByteBuffer buf) throws IOException {
    if (DEBUG) {
        logger.log(Logger.INFO, Thread.currentThread() + " processData:buf.remaining=" + buf.remaining());
    }
    List<Packet> packetList = null;
    while (buf.hasRemaining()) {
        synchronized (packetLock) {
            if (packetPending != null) {
                try {
                    if (packetPending.readPacket(buf)) {
                        if (!packetPending.hasBigPacketException()) {
                            if (packetList == null) {
                                packetList = new ArrayList<>();
                            }
                            packetList.add(packetPending);
                            if (DEBUG) {
                                logger.log(logger.INFO, Thread.currentThread() + "JMSWebSocket@" + hashCode() + " processData(): READ pending PACKET=" + packetPending + ", buf.remaining=" + buf.remaining());
                            }
                        }
                        packetPending = null;
                    }
                } catch (BigPacketException e) {
                    logger.log(logger.ERROR, Thread.currentThread() + "readPacket: " + e.getMessage(), e);
                    WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
                    // XXopt close conn if too big
                    conn.handleBigPacketException(packetPending, e);
                } catch (IllegalArgumentException e) {
                    WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
                    conn.handleIllegalArgumentExceptionPacket(packetPending, e);
                } catch (OutOfMemoryError err) {
                    // XXopt close conn
                    Globals.handleGlobalError(err, Globals.getBrokerResources().getKString(BrokerResources.M_LOW_MEMORY_READALLOC) + ": " + packetPending.headerToString());
                }
                continue;
            }
        }
        if (packetList == null) {
            packetList = new ArrayList<>();
        }
        Packet packet = new Packet(false);
        packet.generateSequenceNumber(false);
        packet.generateTimestamp(false);
        try {
            if (packet.readPacket(buf)) {
                if (!packet.hasBigPacketException()) {
                    packetList.add(packet);
                    if (DEBUG) {
                        logger.log(logger.INFO, Thread.currentThread() + "JMSWebSocket@" + hashCode() + " processData(): READ a PACKET=" + packet);
                    }
                }
            } else {
                synchronized (packetLock) {
                    packetPending = packet;
                }
            }
        } catch (BigPacketException e) {
            logger.log(logger.ERROR, "readPacket: " + e.getMessage(), e);
            WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
            // XXopt close conn if too big
            conn.handleBigPacketException(packet, e);
        } catch (IllegalArgumentException e) {
            logger.log(logger.ERROR, "readPacket: " + e.getMessage(), e);
            WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
            conn.handleIllegalArgumentExceptionPacket(packet, e);
        } catch (OutOfMemoryError err) {
            // XXopt close conn
            Globals.handleGlobalError(err, Globals.getBrokerResources().getKString(BrokerResources.M_LOW_MEMORY_READALLOC) + ": " + packet.headerToString());
        }
        continue;
    }
    if (packetList == null || packetList.isEmpty()) {
        packetList = null;
        return;
    }
    if (DEBUG) {
        logger.log(logger.INFO, "[JMSWebSocket@" + this.hashCode() + "]processData() after processed buf: remaining=" + buf.remaining());
    }
    WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
    for (int i = 0; i < packetList.size(); i++) {
        try {
            final Packet packet = packetList.get(i);
            conn.receivedPacket(packet);
            conn.readData();
        } catch (BrokerException e) {
            // XXclean
            Globals.getLogger().logStack(Logger.ERROR, "Failed to process packet from connection " + this, e);
        }
    }
    packetList.clear();
    packetList = null;
}
Also used : HttpRequestPacket(org.glassfish.grizzly.http.HttpRequestPacket) Packet(com.sun.messaging.jmq.io.Packet) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) BigPacketException(com.sun.messaging.jmq.io.BigPacketException)

Example 20 with Packet

use of com.sun.messaging.jmq.io.Packet in project openmq by eclipse-ee4j.

the class GrizzlyMQPacketDispatchFilter method handleRead.

@Override
public NextAction handleRead(final FilterChainContext ctx) throws IOException {
    final Connection connection = ctx.getConnection();
    final GrizzlyMQPacketList packetList = ctx.getMessage();
    final List<Packet> list = packetList.getPackets();
    GrizzlyMQIPConnection conn = connAttr.get(connection);
    try {
        for (int i = 0; i < list.size(); i++) {
            final Packet packet = list.get(i);
            if (packet == null) {
                Globals.getLogger().log(Logger.ERROR, "Read null packet from connection " + connection);
                throw new IOException("Null Packet");
            }
            conn.receivedPacket(packet);
            conn.readData();
        }
    } catch (BrokerException e) {
        Globals.getLogger().logStack(Logger.ERROR, "Failed to process packet from connection " + connection, e);
        throw new IOException(e.getMessage(), e);
    } finally {
        // @TODO investigate. we can dispose buffer, only if nobody still use it asynchronously.
        packetList.recycle(true);
    // packetList.recycle(false);
    }
    return ctx.getInvokeAction();
}
Also used : Packet(com.sun.messaging.jmq.io.Packet) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) Connection(org.glassfish.grizzly.Connection) IOException(java.io.IOException)

Aggregations

Packet (com.sun.messaging.jmq.io.Packet)57 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)17 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)9 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)8 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)8 IOException (java.io.IOException)7 Hashtable (java.util.Hashtable)7 InvalidPacketException (com.sun.messaging.jmq.io.InvalidPacketException)6 Iterator (java.util.Iterator)6 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)5 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)5 SizeString (com.sun.messaging.jmq.util.SizeString)5 BrokerStateHandler (com.sun.messaging.jmq.jmsserver.BrokerStateHandler)4 HAMonitorService (com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService)4 TransactionAcknowledgement (com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement)4 InvalidSysMessageIDException (com.sun.messaging.jmq.io.InvalidSysMessageIDException)3 PacketReadEOFException (com.sun.messaging.jmq.io.PacketReadEOFException)3 ReadWritePacket (com.sun.messaging.jmq.io.ReadWritePacket)3 PacketReference (com.sun.messaging.jmq.jmsserver.core.PacketReference)3 ByteBuffer (java.nio.ByteBuffer)3