Search in sources :

Example 1 with Message

use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Message in project narayana by jbosstm.

the class SessionImpl method tprecv.

/**
 * Received the next response in a conversation
 *
 * @param flags
 *            The flags to use
 * @return The next response
 * @throws ConnectionException
 *             If the message cannot be received or the flags are incorrect
 * @throws ConfigurationException
 */
public Buffer tprecv(int flags) throws ConnectionException, ConfigurationException {
    log.debug("Receiving: " + cd);
    if (closed) {
        throw new ConnectionException(ConnectionImpl.TPEPROTO, "Session already closed");
    }
    int toCheck = flags & ~(ConnectionImpl.TPNOCHANGE | ConnectionImpl.TPNOBLOCK | ConnectionImpl.TPNOTIME | ConnectionImpl.TPSIGRSTRT);
    if (toCheck != 0) {
        log.trace("invalid flags remain: " + toCheck);
        throw new ConnectionException(ConnectionImpl.TPEINVAL, "Invalid flags remain: " + toCheck);
    }
    if (!canRecv) {
        throw new ConnectionException(ConnectionImpl.TPEPROTO, "Session can't receive");
    }
    Message m = receiver.receive(flags);
    // Prepare the outbound channel
    if (m.replyTo == null || (sender != null && !m.replyTo.equals(sender.getSendTo()))) {
        log.trace("Send to location has altered");
        sender.close();
        sender = null;
    }
    if (sender == null && m.replyTo != null && !m.replyTo.equals("")) {
        log.trace("Will require a new sender");
        if (((String) m.replyTo).contains("IOR:")) {
            sender = transport.createSender(m.replyTo);
        } else {
            sender = transport.createSender(receiver);
        }
    } else {
        log.debug("Not setting the sender");
    }
    BufferImpl received = null;
    if (m.type != null && !m.type.equals("")) {
        received = (BufferImpl) connection.tpalloc(m.type, m.subtype);
        received.deserialize(m.data);
    }
    log.debug("Prepared and ready to launch");
    // Sort out session state
    if ((m.flags & ConnectionImpl.TPRECVONLY) == ConnectionImpl.TPRECVONLY) {
        canSend = true;
        canRecv = false;
    }
    // Check the condition of the response
    if ((m.flags & ConnectionImpl.TPRECVONLY) == ConnectionImpl.TPRECVONLY) {
        throw new ResponseException(ConnectionImpl.TPEEVENT, "Reporting send only event", ConnectionImpl.TPEV_SENDONLY, m.rcode, received);
    } else if (m.rval == EventListener.DISCON_CODE) {
        close();
        throw new ResponseException(ConnectionImpl.TPEEVENT, "Received a disconnect event", ConnectionImpl.TPEV_DISCONIMM, m.rcode, received);
    } else if (m.rval == ConnectionImpl.TPSUCCESS || m.rval == ConnectionImpl.TPFAIL) {
        log.debug("Completed session is being closed: " + cd);
        close();
        if (m.rval == ConnectionImpl.TPSUCCESS) {
            throw new ResponseException(ConnectionImpl.TPEEVENT, "Service completed successfully event", ConnectionImpl.TPEV_SVCSUCC, 0, received);
        } else if (m.rcode == ConnectionImpl.TPESVCERR) {
            throw new ResponseException(ConnectionImpl.TPEEVENT, "Service received an error", ConnectionImpl.TPEV_SVCERR, m.rcode, received);
        } else {
            throw new ResponseException(ConnectionImpl.TPEEVENT, "Service received a fail", ConnectionImpl.TPEV_SVCFAIL, m.rcode, received);
        }
    }
    return received;
}
Also used : Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) ResponseException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 2 with Message

use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Message in project narayana by jbosstm.

the class StompReceiverImpl method receive.

public Message receive(long flagsIn) throws ConnectionException {
    log.debug("Receiving from: " + destinationName);
    org.jboss.narayana.blacktie.jatmibroker.core.transport.hybrid.stomp.Message receive = pendingMessage;
    pendingMessage = null;
    try {
        if (receive == null) {
            receive = StompManagement.receive(socket, inputStream);
            // TODO remove when moving to HQStomp
            if (receive != null && receive.getCommand().equals("RECEIPT") && ignoreSingleReceipt) {
                ignoreSingleReceipt = false;
                receive = StompManagement.receive(socket, inputStream);
            }
            log.debug("Received from: " + destinationName);
        }
        if (receive == null) {
            log.debug("No message to return: " + destinationName);
            return null;
        }
        if (!receive.getCommand().equals("MESSAGE")) {
            throw new ConnectionException(Connection.TPESYSTEM, "Internal error, received unexpected receipt");
        }
        Message convertFromBytesMessage = convertFromBytesMessage(receive);
        convertFromBytesMessage.setOutputStream(outputStream);
        convertFromBytesMessage.setMessageId(receive.getHeaders().get("message-id"));
        log.debug("Returning message from: " + destinationName);
        return convertFromBytesMessage;
    } catch (ConnectionException e) {
        throw e;
    } catch (Exception t) {
        log.debug("Couldn't receive the message: " + t.getMessage(), t);
        throw new ConnectionException(Connection.TPESYSTEM, "Couldn't receive the message", t);
    }
}
Also used : Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException) IOException(java.io.IOException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 3 with Message

use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Message in project narayana by jbosstm.

the class SocketReceiverImpl method receive.

public Message receive(long flags) throws ConnectionException {
    if (closed) {
        throw new ConnectionException(Connection.TPEPROTO, "Receiver already closed");
    }
    Message message = null;
    if ((flags & Connection.TPNOBLOCK) != Connection.TPNOBLOCK) {
        if (server != null) {
            message = server.receiveMessage(cd, determineTimeout(flags));
        } else if (socket != null) {
            synchronized (this) {
                if ((flags & Connection.TPNOBLOCK) != Connection.TPNOBLOCK) {
                    if (data.isEmpty()) {
                        try {
                            wait(determineTimeout(flags));
                        } catch (InterruptedException e) {
                        }
                    }
                }
                if (!data.isEmpty()) {
                    message = data.remove(0);
                }
            }
        }
    } else {
        log.debug("Not waiting for the response, hope its there!");
    }
    if (message == null && (flags & Connection.TPNOBLOCK) == Connection.TPNOBLOCK) {
        throw new ConnectionException(Connection.TPEBLOCK, "Did not receive a message");
    } else if (message == null) {
        if (JtsTransactionImple.hasTransaction()) {
            try {
                log.debug("Marking rollbackOnly");
                TransactionImpl.current().rollback_only();
            } catch (TransactionException e) {
                throw new ConnectionException(Connection.TPESYSTEM, "Could not mark transaction for rollback only");
            }
        }
        throw new ConnectionException(Connection.TPETIME, "Did not receive a message");
    } else {
        log.debug("Message was available");
        if (message.rval == EventListener.DISCON_CODE) {
            if (TransactionImpl.current() != null) {
                try {
                    log.debug("Marking rollbackOnly as disconnection");
                    TransactionImpl.current().rollback_only();
                } catch (TransactionException e) {
                    throw new ConnectionException(Connection.TPESYSTEM, "Could not mark transaction for rollback only");
                }
            }
        } else if (message.rcode == Connection.TPESVCERR) {
            if (TransactionImpl.current() != null) {
                try {
                    log.debug("Marking rollbackOnly as svc err");
                    TransactionImpl.current().rollback_only();
                } catch (TransactionException e) {
                    throw new ConnectionException(Connection.TPESYSTEM, "Could not mark transaction for rollback only");
                }
            }
        } else if (message.rval == Connection.TPFAIL) {
            if (TransactionImpl.current() != null) {
                try {
                    TransactionImpl.current().rollback_only();
                } catch (TransactionException e) {
                    throw new ConnectionException(Connection.TPESYSTEM, "Could not mark transaction for rollback only");
                }
            }
        }
    }
    if (responseMonitor != null) {
        responseMonitor.responseReceived(cd, true);
    }
    return message;
}
Also used : TransactionException(org.jboss.narayana.blacktie.jatmibroker.core.tx.TransactionException) Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 4 with Message

use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Message in project narayana by jbosstm.

the class ServiceDispatcher method run.

public void run() {
    log.debug("Running");
    while (!closing) {
        Message message = null;
        try {
            message = receiver.receive(0);
            log.trace("Received");
            if (message != null && !closing) {
                // Process the consumed message
                try {
                    this.processMessage(serviceName, message);
                    log.trace("Processed");
                } catch (Throwable t) {
                    log.error("Can't process the message", t);
                }
                try {
                    // Assumes the message was received from stomp -
                    // fair
                    // assumption outside of an MDB
                    message.ack();
                } catch (IOException t) {
                    log.error("Can't ack the message", t);
                }
            }
        } catch (ConnectionException e) {
            if (closing) {
                log.trace("Got an exception during close: " + e.getMessage(), e);
                break;
            }
            if (e.getTperrno() == Connection.TPETIME) {
                log.debug("Got a timeout");
            } else {
                log.error("Could not receive the message: " + e.getMessage(), e);
                break;
            }
        } catch (Throwable t) {
            log.warn("Got throwable trying to receive: " + t.getMessage(), t);
        }
    }
    synchronized (closeLock) {
        log.debug("Close the thread");
        closed = true;
        closeLock.notify();
    }
}
Also used : Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) IOException(java.io.IOException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 5 with Message

use of org.jboss.narayana.blacktie.jatmibroker.core.transport.Message in project narayana by jbosstm.

the class TestSocketServer method test.

@Test
public void test() throws Exception {
    server.register(1, null, null);
    Socket socket = new Socket("localhost", port);
    DataOutputStream outs = new DataOutputStream(socket.getOutputStream());
    int sid = 1;
    int correlationId = 0;
    int rcode = 0;
    int flags = 0;
    int rval = 0;
    int len = 4;
    byte[] toSend = new byte[len];
    toSend[0] = 'a';
    String toReplyTo = "(null)";
    String type = "X_OCTET";
    String subtype = "(null)";
    send(outs, sid, correlationId, rcode, toSend, flags, rval, toReplyTo, type, subtype);
    send(outs, sid, correlationId, 1, toSend, 1, 1, "test", type, subtype);
    Message msg = server.receiveMessage(sid, 2000);
    assertTrue(msg != null);
    assertTrue(msg.cd == 0);
    assertTrue(msg.type.equals(type));
    assertTrue(msg.len == len);
    assertTrue(msg.rcode == 0);
    assertTrue(msg.flags == 0);
    assertTrue(msg.subtype == null);
    assertTrue(msg.data[0] == toSend[0]);
    assertTrue(msg.rval == 0);
    assertTrue(msg.replyTo == null);
    msg = server.receiveMessage(sid, 2000);
    assertTrue(msg != null);
    assertTrue(msg.rcode == 1);
    outs.close();
    socket.close();
    server.unregister(1);
}
Also used : Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) DataOutputStream(java.io.DataOutputStream) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

Message (org.jboss.narayana.blacktie.jatmibroker.core.transport.Message)11 ConnectionException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)5 IOException (java.io.IOException)4 Receiver (org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver)3 DataInputStream (java.io.DataInputStream)2 EOFException (java.io.EOFException)2 SocketException (java.net.SocketException)2 Sender (org.jboss.narayana.blacktie.jatmibroker.core.transport.Sender)2 ResponseException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException)2 DataOutputStream (java.io.DataOutputStream)1 Socket (java.net.Socket)1 Properties (java.util.Properties)1 CodecFactory (org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory)1 ResponseMonitor (org.jboss.narayana.blacktie.jatmibroker.core.ResponseMonitor)1 AtmiBrokerEnvXML (org.jboss.narayana.blacktie.jatmibroker.core.conf.AtmiBrokerEnvXML)1 Codec (org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec)1 EventListener (org.jboss.narayana.blacktie.jatmibroker.core.transport.EventListener)1 Transport (org.jboss.narayana.blacktie.jatmibroker.core.transport.Transport)1 TransportFactory (org.jboss.narayana.blacktie.jatmibroker.core.transport.TransportFactory)1 TransactionException (org.jboss.narayana.blacktie.jatmibroker.core.tx.TransactionException)1