Search in sources :

Example 36 with ConnectionException

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

the class ConnectionImpl method tpacall.

/**
 * Asynchronous call
 *
 * @param svc
 *            The name of the service to call
 * @param toSend
 *            The inbound data
 * @param flags
 *            The flags to use
 * @return The connection descriptor
 * @throws ConnectionException
 *             If the service cannot be contacted.
 */
public int tpacall(String svc, Buffer toSend, int flags) throws ConnectionException {
    log.debug("tpacall");
    int toCheck = flags & ~(TPNOTRAN | TPNOREPLY | TPNOBLOCK | TPNOTIME | TPSIGRSTRT);
    if (toCheck != 0) {
        log.trace("invalid flags remain: " + toCheck);
        throw new ConnectionException(ConnectionImpl.TPEINVAL, "Invalid flags remain: " + toCheck);
    }
    svc = svc.substring(0, Math.min(ConnectionImpl.XATMI_SERVICE_NAME_LENGTH, svc.length()));
    String qtype = (String) properties.get("blacktie." + svc + ".type");
    log.debug(svc + " qtype is " + qtype + " and flags is " + flags);
    if ("topic".equals(qtype) && (flags & TPNOREPLY) == 0) {
        log.warn(svc + " type is " + qtype + " and MUST have TPNOREPLY set");
        throw new ConnectionException(ConnectionImpl.TPEINVAL, svc + " type is " + qtype + " and MUST have TPNOREPLY set");
    }
    int correlationId = 0;
    synchronized (this) {
        correlationId = ++nextId;
        log.trace("Allocated next sessionId: " + correlationId);
    }
    Transport transport = getTransport(svc);
    Receiver endpoint = transport.createReceiver(correlationId, responseMonitor, null);
    temporaryQueues.put(correlationId, endpoint);
    log.trace("Added a queue for: " + correlationId);
    // TODO HANDLE TRANSACTION
    String type = null;
    String subtype = null;
    int len = 0;
    byte[] data = null;
    if (toSend != null) {
        CodecFactory factory = new CodecFactory(this);
        String coding_type = properties.getProperty("blacktie." + svc + ".coding_type");
        Codec codec = factory.getCodec(coding_type);
        data = codec.encode((BufferImpl) toSend);
        // data = toSend.serialize();
        type = toSend.getType();
        subtype = toSend.getSubtype();
        len = toSend.getLen();
    }
    String timeToLive = properties.getProperty("TimeToLive");
    int ttl = 0;
    // Don't set ttl when tpacall and TPNOREPLY set
    if (timeToLive != null && ((flags & ConnectionImpl.TPNOREPLY) != ConnectionImpl.TPNOREPLY)) {
        ttl = Integer.parseInt(timeToLive) * 1000;
        log.debug("Set ttl: " + ttl);
    }
    transport.getSender(svc, false).send(endpoint.getReplyTo(), (short) 0, 0, data, len, correlationId, flags, ttl, type, subtype);
    if ((flags & ConnectionImpl.TPNOREPLY) == ConnectionImpl.TPNOREPLY) {
        correlationId = 0;
    }
    log.debug("Returning cd: " + correlationId);
    return correlationId;
}
Also used : Codec(org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec) Receiver(org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver) CodecFactory(org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory) Transport(org.jboss.narayana.blacktie.jatmibroker.core.transport.Transport) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 37 with ConnectionException

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

the class ConnectionImpl method tpconnect.

/**
 * Handle the initiation of a conversation with the server.
 *
 * @param svc
 *            The name of the service
 * @param toSend
 *            The outbound buffer
 * @param flags
 *            The flags to use
 * @return The connection descriptor
 * @throws ConnectionException
 *             If the service cannot be contacted.
 */
public Session tpconnect(String svc, Buffer toSend, int flags) throws ConnectionException {
    log.debug("tpconnect: " + svc);
    svc = svc.substring(0, Math.min(ConnectionImpl.XATMI_SERVICE_NAME_LENGTH, svc.length()));
    // Initiate the session
    svc = svc.substring(0, Math.min(ConnectionImpl.XATMI_SERVICE_NAME_LENGTH, svc.length()));
    int correlationId = 0;
    synchronized (this) {
        correlationId = nextId++;
    }
    Transport transport = getTransport(svc);
    SessionImpl session = new SessionImpl(this, svc, transport, correlationId);
    Receiver receiver = session.getReceiver();
    // TODO HANDLE TRANSACTION
    String type = null;
    String subtype = null;
    int len = 0;
    byte[] data = null;
    if (toSend != null) {
        CodecFactory factory = new CodecFactory(this);
        String coding_type = properties.getProperty("blacktie." + svc + ".coding_type");
        Codec codec = factory.getCodec(coding_type);
        data = codec.encode((BufferImpl) toSend);
        // data = toSend.serialize();
        type = toSend.getType();
        subtype = toSend.getSubtype();
        len = toSend.getLen();
    }
    String timeToLive = properties.getProperty("TimeToLive");
    int ttl = 0;
    if (timeToLive != null) {
        ttl = Integer.parseInt(timeToLive) * 1000;
    }
    log.debug("tpconnect sending data");
    session.getSender().send(receiver.getReplyTo(), (short) 0, 0, data, len, correlationId, flags | TPCONV, ttl, type, subtype);
    byte[] response = null;
    try {
        log.debug("tpconnect receiving data");
        X_OCTET_Impl odata = (X_OCTET_Impl) session.tprecv(0);
        // TODO THIS SHOULD BE A BETTER ERROR AND CHECKED IF TPCONV AND NOT
        // CONV
        response = odata.getByteArray();
        log.debug("tpconnect received data");
    } catch (ResponseException e) {
        response = ((X_OCTET_Impl) e.getReceived()).getByteArray();
        log.debug("Caught an exception with data", e);
    } catch (ConnectionException e) {
        session.close();
        throw new ConnectionException(e.getTperrno(), "Could not connect");
    } catch (ConfigurationException e) {
        session.close();
        throw new ConnectionException(ConnectionImpl.TPEOS, "Configuration exception: " + e.getMessage(), e);
    }
    byte[] ack = new byte[4];
    byte[] bytes = "ACK".getBytes();
    System.arraycopy(bytes, 0, ack, 0, 3);
    boolean connected = response == null ? false : Arrays.equals(ack, response);
    if (!connected) {
        log.error("Could not connect");
        session.close();
        throw new ConnectionException(ConnectionImpl.TPESYSTEM, "Could not connect");
    }
    session.setCreatorState(flags);
    sessions.put(correlationId, session);
    log.trace("Added session: " + correlationId);
    // Return a handle to allow the connection to send/receive data on
    log.debug("Created session: " + correlationId);
    return session;
}
Also used : ResponseException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException) Receiver(org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver) Codec(org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec) ConfigurationException(org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException) CodecFactory(org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory) Transport(org.jboss.narayana.blacktie.jatmibroker.core.transport.Transport) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 38 with ConnectionException

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

the class ConnectionImpl method receive.

/**
 * Retrieve a response.
 *
 * @param cd
 *            The connection descriptor
 * @param flags
 *            The flags to use
 * @return The response
 * @throws ConnectionException
 *             If the response cannot be retrieved.
 * @throws ConfigurationException
 */
private Response receive(int cd, int flags) throws ConnectionException, ConfigurationException {
    log.debug("receive: " + cd);
    Receiver endpoint = temporaryQueues.get(cd);
    if (endpoint == null) {
        throw new ConnectionException(ConnectionImpl.TPEBADDESC, "Session does not exist: " + cd);
    }
    Message message = endpoint.receive(flags);
    Buffer buffer = null;
    if (message.type != null && !message.type.equals("")) {
        CodecFactory factory = new CodecFactory(this);
        String coding_type = properties.getProperty("blacktie." + message.serviceName + ".coding_type");
        Codec codec = factory.getCodec(coding_type);
        buffer = codec.decode(message.type, message.subtype, message.data, message.len);
    // buffer = tpalloc(message.type, message.subtype, message.len);
    // buffer.deserialize(message.data);
    }
    if (message.rval == ConnectionImpl.TPFAIL) {
        if (message.rcode == ConnectionImpl.TPESVCERR) {
            throw new ResponseException(ConnectionImpl.TPESVCERR, "Got an error back from the remote service", -1, message.rcode, buffer);
        }
        throw new ResponseException(ConnectionImpl.TPESVCFAIL, "Got a fail back from the remote service", -1, message.rcode, buffer);
    } else {
        Response response = new Response(cd, message.rval, message.rcode, buffer, message.flags);
        log.debug("received returned a response? " + (response == null ? "false" : "true"));
        return response;
    }
}
Also used : Buffer(org.jboss.narayana.blacktie.jatmibroker.xatmi.Buffer) Response(org.jboss.narayana.blacktie.jatmibroker.xatmi.Response) Codec(org.jboss.narayana.blacktie.jatmibroker.core.transport.Codec) Message(org.jboss.narayana.blacktie.jatmibroker.core.transport.Message) ResponseException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ResponseException) Receiver(org.jboss.narayana.blacktie.jatmibroker.core.transport.Receiver) CodecFactory(org.jboss.narayana.blacktie.jatmibroker.codec.CodecFactory) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 39 with ConnectionException

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

the class ConnectionImpl method tpgetrply.

/**
 * Get the reply for an asynchronous call.
 *
 * @param cd
 *            The connection descriptor to use
 * @param flags
 *            The flags to use
 * @return The response from the server
 * @throws ConnectionException
 *             If the service cannot be contacted.
 * @throws ConfigurationException
 */
public Response tpgetrply(int cd, int flags) throws ConnectionException, ConfigurationException {
    log.debug("tpgetrply: " + cd);
    int toCheck = flags & ~(TPGETANY | TPNOCHANGE | TPNOBLOCK | TPNOTIME | TPSIGRSTRT);
    if (toCheck != 0) {
        log.trace("invalid flags remain: " + toCheck);
        throw new ConnectionException(ConnectionImpl.TPEINVAL, "Invalid flags remain: " + toCheck);
    }
    synchronized (tpGetAnySessions) {
        if ((flags & ConnectionImpl.TPGETANY) == ConnectionImpl.TPGETANY) {
            if ((flags & ConnectionImpl.TPNOBLOCK) != ConnectionImpl.TPNOBLOCK) {
                int timeout = 0;
                if ((flags & ConnectionImpl.TPNOTIME) != ConnectionImpl.TPNOTIME) {
                    timeout = Integer.parseInt(properties.getProperty("ReceiveTimeout")) * 1000 + Integer.parseInt(properties.getProperty("TimeToLive")) * 1000;
                }
                if (tpGetAnySessions.size() == 0) {
                    try {
                        tpGetAnySessions.wait(timeout);
                    } catch (InterruptedException e) {
                        throw new ConnectionException(ConnectionImpl.TPESYSTEM, "Could not wait", e);
                    }
                }
                if (tpGetAnySessions.size() == 0) {
                    throw new ConnectionException(ConnectionImpl.TPETIME, "No message arrived");
                }
            } else if (tpGetAnySessions.size() == 0) {
                throw new ConnectionException(ConnectionImpl.TPEBLOCK, "No message arrived");
            }
            cd = tpGetAnySessions.remove(0);
        }
    }
    Response toReturn = receive(cd, flags);
    tpcancel(cd);
    log.debug("tpgetrply returning: " + toReturn);
    return toReturn;
}
Also used : Response(org.jboss.narayana.blacktie.jatmibroker.xatmi.Response) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException)

Example 40 with ConnectionException

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

the class MDBBlacktieService method onMessage.

/**
 * The onMessage method formats the JMS received bytes message into a format understood by the XATMI API.
 *
 * @param message The message received wrapping an XATMI invocation
 */
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void onMessage(Message message) {
    try {
        String serviceName = null;
        Destination jmsDestination = message.getJMSDestination();
        if (jmsDestination instanceof Queue) {
            serviceName = ((Queue) jmsDestination).getQueueName();
        } else {
            serviceName = ((Topic) jmsDestination).getTopicName();
        }
        serviceName = serviceName.substring(serviceName.indexOf('_') + 1);
        log.trace(serviceName);
        if (JtsTransactionImple.hasTransaction()) {
            throw new ConnectionException(Connection.TPEPROTO, "Blacktie services must not be called with a transactional context: " + getName() + ":" + serviceName);
        }
        BytesMessage bytesMessage = ((BytesMessage) message);
        org.jboss.narayana.blacktie.jatmibroker.core.transport.Message toProcess = convertFromBytesMessage(bytesMessage);
        log.debug("SERVER onMessage: transaction control ior: " + toProcess.control);
        processMessage(serviceName, toProcess);
        log.debug("Processed message");
    } catch (Throwable t) {
        log.error("Could not service the request", t);
    }
}
Also used : Destination(javax.jms.Destination) BytesMessage(javax.jms.BytesMessage) Queue(javax.jms.Queue) ConnectionException(org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException) TransactionAttribute(javax.ejb.TransactionAttribute)

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