Search in sources :

Example 16 with ConnectionException

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

the class StompSenderImpl method send.

/**
 * Don't want send and close at the same time
 */
public synchronized void send(Object replyTo, short rval, int rcode, byte[] data, int len, int correlationId, int flags, int ttl, String type, String subtype) throws ConnectionException {
    if (closed) {
        throw new ConnectionException(Connection.TPEPROTO, "Sender closed");
    }
    if (data == null) {
        data = new byte[1];
        len = 1;
    }
    if (len < 1) {
        throw new ConnectionException(Connection.TPEINVAL, "Length of buffer must be greater than 0");
    }
    log.debug("Sender sending: " + destinationName);
    Message message = new Message();
    message.setCommand("SEND");
    Map<String, String> headers = new HashMap<String, String>();
    try {
        String ior = JtsTransactionImple.getTransactionIOR();
        if (ior != null) {
            headers.put("messagecontrol", ior);
            log.debug("Sender sending IOR: " + ior);
        }
    } catch (Exception e) {
        throw new ConnectionException(Connection.TPETRAN, e.getMessage());
    }
    if (replyTo != null) {
        log.debug("Reply to: " + replyTo);
        headers.put("messagereplyto", (String) replyTo);
    }
    headers.put("servicename", destinationName);
    headers.put("messagecorrelationId", String.valueOf(correlationId));
    headers.put("messageflags", String.valueOf(flags));
    headers.put("messagerval", String.valueOf(rval));
    headers.put("messagercode", String.valueOf(rcode));
    headers.put("messagetype", type == null ? "" : type);
    headers.put("messagesubtype", subtype == null ? "" : subtype);
    if (ttl > 0) {
        headers.put("expires", String.valueOf(ttl));
        log.debug("EXPIRES: " + headers.get("expires"));
    }
    synchronized (StompSenderImpl.class) {
        headers.put("receipt", "send-J-" + counter);
        log.debug("RECEIPT: " + headers.get("receipt"));
        counter++;
    }
    headers.put("destination", destinationName);
    message.setHeaders(headers);
    byte[] toSend = new byte[len];
    if (data != null) {
        int min = Math.min(toSend.length, data.length);
        System.arraycopy(data, 0, toSend, 0, min);
        headers.put("content-length", String.valueOf(toSend.length));
    }
    message.setBody(toSend);
    Message ack;
    try {
        StompManagement.send(message, this.outputStream);
        ack = StompManagement.receive(socket, this.inputStream);
    } catch (IOException e) {
        throw new ConnectionException(Connection.TPEOS, e.getMessage());
    }
    if (!ack.getCommand().equals("RECEIPT")) {
        log.error(new String(ack.getBody()));
        throw new ConnectionException(Connection.TPENOENT, new String(ack.getBody()));
    }
    log.debug("sent message");
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException) IOException(java.io.IOException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 17 with ConnectionException

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

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

the class BlacktieStompAdministrationService method tpservice.

public Response tpservice(TPSVCINFO svcinfo) {
    log.debug("Message received");
    X_OCTET recv = (X_OCTET) svcinfo.getBuffer();
    String string = new String(recv.getByteArray());
    StringTokenizer st = new StringTokenizer(string, ",", false);
    String operation = st.nextToken();
    String serverName = st.nextToken();
    String serviceName = st.nextToken();
    byte[] success = new byte[1];
    String server = null;
    try {
        if (serviceName.indexOf(".") > -1) {
            server = serviceName.substring(1);
            server = server.replaceAll("[0-9]", "");
        } else {
            ServerInfo info = SERVICE_OWNERS.get(serviceName);
            if (info == null) {
                server = serverName;
            } else {
                server = info.name;
            }
        }
        if (server != null && server.equals(serverName)) {
            log.trace("Service " + serviceName + " exists for server: " + server);
            if (operation.equals("tpadvertise")) {
                log.trace("Advertising: " + serviceName);
                boolean conversational = st.nextToken().equals("1");
                String type = st.nextToken();
                String version = st.nextToken();
                success[0] = (byte) deployQueue(serviceName, serverName, conversational, type, version);
                log.trace("Advertised: " + serviceName);
            } else if (operation.equals("decrementconsumer")) {
                log.trace("Decrement consumer: " + serviceName);
                success[0] = (byte) decrementConsumer(serviceName);
                log.trace("Decremented consumer: " + serviceName);
            } else {
                log.error("Unknown operation " + operation);
                success[0] = 0;
            }
        } else {
            log.error("Service " + serviceName + " already exists for a different server (" + server + ")");
            success[0] = 0;
        }
        X_OCTET buffer = (X_OCTET) svcinfo.getConnection().tpalloc("X_OCTET", null);
        buffer.setByteArray(success);
        log.debug("Responding");
        return new Response(Connection.TPSUCCESS, 0, buffer, 0);
    } catch (ConnectionException e) {
        return new Response(Connection.TPFAIL, 0, null, 0);
    } catch (ConfigurationException e) {
        return new Response(Connection.TPFAIL, 0, null, 0);
    } catch (UnknownHostException e) {
        return new Response(Connection.TPFAIL, 0, null, 0);
    }
}
Also used : Response(org.jboss.narayana.blacktie.jatmibroker.xatmi.Response) X_OCTET(org.jboss.narayana.blacktie.jatmibroker.xatmi.X_OCTET) StringTokenizer(java.util.StringTokenizer) UnknownHostException(java.net.UnknownHostException) ConfigurationException(org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 19 with ConnectionException

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

the class AdministrationProxy method getServiceCounterById.

public long getServiceCounterById(String serverName, int id, String serviceName) {
    log.trace("getServiceCounterById");
    long counter = 0;
    String command = "counter," + serviceName + ",";
    try {
        Response buf = callAdminService(serverName, id, command);
        if (buf != null) {
            byte[] received = ((X_OCTET) buf.getBuffer()).getByteArray();
            counter = Long.parseLong(new String(received, 1, received.length - 1));
        }
    } catch (ConnectionException e) {
        log.error("call server " + serverName + " id " + id + " failed with " + e.getTperrno());
    } catch (ConfigurationException e) {
        log.error("call server " + serverName + " id " + id + " failed with configuration exception", e);
    }
    return counter;
}
Also used : Response(org.jboss.narayana.blacktie.jatmibroker.xatmi.Response) X_OCTET(org.jboss.narayana.blacktie.jatmibroker.xatmi.X_OCTET) ConfigurationException(org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 20 with ConnectionException

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

the class AdministrationProxy method shutdown.

public Boolean shutdown(String serverName, int id) {
    log.trace("shutdown");
    List<String> servers = listRunningServers();
    if (servers.contains(serverName)) {
        String command = "serverdone";
        boolean shutdown = false;
        try {
            if (id == 0) {
                List<Integer> ids = listRunningInstanceIds(serverName);
                ConnectionException toRethrow = null;
                for (int i = 0; i < ids.size(); i++) {
                    try {
                        callAdminService(serverName, ids.get(i), command);
                    } catch (ConnectionException e) {
                        log.error("call server " + serverName + " id " + id + " failed with " + e.getTperrno(), e);
                        if (e.getTperrno() == org.jboss.narayana.blacktie.jatmibroker.xatmi.Connection.TPETIME) {
                            callAdminService(serverName, ids.get(i), command);
                        } else {
                            toRethrow = e;
                        }
                    } catch (ConfigurationException e) {
                        log.error("call server " + serverName + " id " + id + " failed with configuration exception", e);
                        toRethrow = new ConnectionException(Connection.TPEOS, "Configuration issue: " + e.getMessage(), e);
                    }
                }
                if (toRethrow != null) {
                    throw toRethrow;
                }
            } else {
                callAdminService(serverName, id, command);
            }
            int timeout = 40;
            while (true) {
                List<Integer> ids = listRunningInstanceIds(serverName);
                if (id == 0 && ids.size() > 0 || ids.contains(id)) {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    timeout--;
                } else {
                    shutdown = true;
                    break;
                }
                if (timeout == 0) {
                    log.warn("Server did not shutdown in time: " + serverName + ": " + id);
                    break;
                }
            }
            return shutdown;
        } catch (ConnectionException e) {
            log.error("call server " + serverName + " id " + id + " failed with " + e.getTperrno(), e);
            return false;
        } catch (RuntimeException e) {
            log.error("Could not shutdown server: " + e.getMessage(), e);
            throw e;
        } catch (ConfigurationException e) {
            log.error("call server " + serverName + " id " + id + " failed with configuration exception", e);
            return false;
        }
    } else {
        log.error("Server not configured: " + serverName);
        return false;
    }
}
Also used : ConfigurationException(org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException) 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