Search in sources :

Example 11 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException in project narayana by jbosstm.

the class SocketSenderImpl method send.

public void send(Object replyTo, short rval, int rcode, byte[] data, int len, int correlationId, int flags, int ttl, String type, String subtype) throws ConnectionException {
    log.debug("Sending the message");
    if (closed) {
        log.error("Sender closed");
        throw new ConnectionException(Connection.TPEPROTO, "Sender closed");
    }
    if (data == null) {
        data = new byte[1];
        data[0] = 0;
        len = 1;
    }
    String toReplyTo = (String) replyTo;
    if (toReplyTo == null || toReplyTo.isEmpty()) {
        log.debug("Reply to set as null");
        toReplyTo = "(null)";
    }
    if (type == null || type.isEmpty()) {
        log.debug("Type set as null");
        type = "(null)";
    }
    if (subtype == null || subtype.isEmpty()) {
        log.debug("Subtype set as null");
        subtype = "(null)";
    }
    if (len < 1) {
        log.error("Length of buffer must be greater than 0");
        throw new ConnectionException(Connection.TPEINVAL, "Length of buffer must be greater than 0");
    }
    byte[] toSend = new byte[len + pad];
    if (data != null) {
        int min = Math.min(toSend.length, data.length);
        System.arraycopy(data, 0, toSend, 0, min);
    }
    try {
        StringBuffer buffer = new StringBuffer();
        buffer.append(sid).append("\n").append(correlationId).append("\n").append(rcode).append("\n").append(toSend.length).append("\n").append(flags).append("\n").append(rval).append("\n").append(toReplyTo).append("\n").append(type).append("\n").append(subtype).append("\n");
        int sendlen = buffer.length() + toSend.length;
        log.debug("send on " + socket + " len is " + sendlen + " and buffer is " + buffer);
        try {
            outs.writeInt(sendlen);
            outs.write(buffer.toString().getBytes(), 0, buffer.length());
            outs.write(toSend, 0, toSend.length);
        } catch (SocketException e) {
            // The socket might be closed by service side
            log.warn("socket send with " + e);
        }
    } catch (IOException e) {
        throw new ConnectionException(Connection.TPEPROTO, "send failed with " + e);
    }
}
Also used : SocketException(java.net.SocketException) IOException(java.io.IOException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 12 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException 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 13 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException 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 14 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException in project narayana by jbosstm.

the class TransportImpl method getReceiver.

public Receiver getReceiver(String serviceName, boolean conversational) throws ConnectionException {
    if (closed) {
        log.error("Already closed");
        throw new ConnectionException(Connection.TPEPROTO, "Already closed");
    }
    log.debug("Creating a receiver: " + serviceName);
    Map<String, Receiver> conversationalMap = receivers.get(conversational);
    if (conversationalMap == null) {
        conversationalMap = new HashMap<String, Receiver>();
        receivers.put(conversational, conversationalMap);
    }
    Receiver toReturn = conversationalMap.get(serviceName);
    if (toReturn == null) {
        try {
            log.debug("Resolved destination");
            String type = (String) properties.get("blacktie." + serviceName + ".type");
            return new StompReceiverImpl(serviceName, conversational, type, properties);
        } catch (ConnectionException e) {
            throw e;
        } catch (Throwable t) {
            throw new ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.Connection.TPESYSTEM, "Could not create the receiver on: " + serviceName, t);
        }
    }
    return toReturn;
}
Also used : StompReceiverImpl(org.jboss.narayana.blacktie.jatmibroker.core.transport.hybrid.stomp.StompReceiverImpl) Receiver(org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 15 with ConnectionException

use of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException in project narayana by jbosstm.

the class StompManagement method connect.

public static Socket connect(String host, int port, String username, String password) throws IOException, ConnectionException {
    Socket socket = new Socket(host, port);
    InputStream inputStream = socket.getInputStream();
    OutputStream outputStream = socket.getOutputStream();
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("login", username);
    headers.put("passcode", password);
    Message message = new Message();
    message.setCommand("CONNECT");
    message.setHeaders(headers);
    send(message, outputStream);
    Message received = receive(socket, inputStream);
    if (received.getCommand().equals("ERROR")) {
        throw new ConnectionException(Connection.TPESYSTEM, new String(received.getBody()));
    }
    log.debug("Created socket: " + socket + " input: " + inputStream + "output: " + outputStream);
    return socket;
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Aggregations

ConnectionException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)41 Response (org.jboss.narayana.blacktie.jatmibroker.xatmi.Response)12 ConfigurationException (org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException)11 IOException (java.io.IOException)9 X_OCTET (org.jboss.narayana.blacktie.jatmibroker.xatmi.X_OCTET)9 ResponseException (org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException)6 Message (org.jboss.narayana.blacktie.jatmibroker.core.transport.Message)5 Receiver (org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver)5 Buffer (org.jboss.narayana.blacktie.jatmibroker.xatmi.Buffer)5 CodecFactory (org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory)3 Codec (org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec)3 Transport (org.jboss.narayana.blacktie.jatmibroker.core.transport.Transport)3 TransactionException (org.jboss.narayana.blacktie.jatmibroker.core.tx.TransactionException)3 SocketException (java.net.SocketException)2 HashMap (java.util.HashMap)2 StringTokenizer (java.util.StringTokenizer)2 ServiceData (org.jboss.narayana.blacktie.jatmibroker.core.server.ServiceData)2 Sender (org.jboss.narayana.blacktie.jatmibroker.core.transport.Sender)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1