Search in sources :

Example 51 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class FtpListener method getRawMessage.

/**
 * Retrieves a single record from a file. If the file is empty or fully processed, it looks wether there
 * is a new file to process and returns the first record.
 */
public synchronized Object getRawMessage(Map threadContext) throws ListenerException {
    log.debug("FtpListener [" + getName() + "] in getRawMessage, retrieving contents of directory [" + remoteDirectory + "]");
    if (remoteFilenames.isEmpty()) {
        try {
            openClient(remoteDirectory);
            List names = ls(remoteDirectory, true, true);
            log.debug("FtpListener [" + getName() + "] received ls result of [" + names.size() + "] files");
            if (names != null && names.size() > 0) {
                remoteFilenames.addAll(names);
            }
        } catch (Exception e) {
            throw new ListenerException("Exception retrieving contents of directory [" + remoteDirectory + "]", e);
        } finally {
            closeClient();
        }
    }
    if (!remoteFilenames.isEmpty()) {
        Object result = remoteFilenames.removeFirst();
        log.debug("FtpListener " + getName() + " returns " + result.toString());
        return result;
    }
    waitAWhile();
    return null;
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) List(java.util.List) LinkedList(java.util.LinkedList) INamedObject(nl.nn.adapterframework.core.INamedObject) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ListenerException(nl.nn.adapterframework.core.ListenerException)

Example 52 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class EsbUtils method receiveMessageAndMoveToErrorStorage.

public static String receiveMessageAndMoveToErrorStorage(EsbJmsListener esbJmsListener, JdbcTransactionalStorage errorStorage) {
    String result = null;
    PoolingConnectionFactory jmsConnectionFactory = null;
    PoolingDataSource jdbcDataSource = null;
    BitronixTransactionManager btm = null;
    javax.jms.Connection jmsConnection = null;
    try {
        jmsConnectionFactory = getPoolingConnectionFactory(esbJmsListener);
        if (jmsConnectionFactory != null) {
            jdbcDataSource = getPoolingDataSource(errorStorage);
            if (jdbcDataSource != null) {
                String instanceNameLc = AppConstants.getInstance().getString("instance.name.lc", null);
                String logDir = AppConstants.getInstance().getString("log.dir", null);
                TransactionManagerServices.getConfiguration().setServerId(instanceNameLc + ".tm");
                TransactionManagerServices.getConfiguration().setLogPart1Filename(logDir + File.separator + instanceNameLc + "-btm1.tlog");
                TransactionManagerServices.getConfiguration().setLogPart2Filename(logDir + File.separator + instanceNameLc + "-btm2.tlog");
                btm = TransactionManagerServices.getTransactionManager();
                jmsConnection = jmsConnectionFactory.createConnection();
                Session jmsSession = null;
                MessageConsumer jmsConsumer = null;
                java.sql.Connection jdbcConnection = null;
                btm.begin();
                log.debug("started transaction [" + btm.getCurrentTransaction().getGtrid() + "]");
                try {
                    jmsSession = jmsConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
                    String queueName = esbJmsListener.getPhysicalDestinationShortName();
                    Queue queue = jmsSession.createQueue(queueName);
                    jmsConsumer = jmsSession.createConsumer(queue);
                    jmsConnection.start();
                    long timeout = 30000;
                    log.debug("looking for message on queue [" + queueName + "] with timeout of [" + timeout + "] msec");
                    Message rawMessage = jmsConsumer.receive(timeout);
                    if (rawMessage == null) {
                        log.debug("no message found on queue [" + queueName + "]");
                    } else {
                        String id = rawMessage.getJMSMessageID();
                        log.debug("found message on queue [" + queueName + "] with messageID [" + id + "]");
                        Serializable sobj = null;
                        if (rawMessage != null) {
                            if (rawMessage instanceof Serializable) {
                                sobj = (Serializable) rawMessage;
                            } else {
                                try {
                                    sobj = new MessageWrapper(rawMessage, esbJmsListener);
                                } catch (ListenerException e) {
                                    log.error("could not wrap non serializable message for messageId [" + id + "]", e);
                                    if (rawMessage instanceof TextMessage) {
                                        TextMessage textMessage = (TextMessage) rawMessage;
                                        sobj = textMessage.getText();
                                    } else {
                                        sobj = rawMessage.toString();
                                    }
                                }
                            }
                        }
                        jdbcConnection = jdbcDataSource.getConnection();
                        result = errorStorage.storeMessage(jdbcConnection, id, id, new Date(System.currentTimeMillis()), "moved message", null, sobj);
                    }
                    log.debug("committing transaction [" + btm.getCurrentTransaction().getGtrid() + "]");
                    btm.commit();
                } catch (Exception e) {
                    if (btm.getCurrentTransaction() != null) {
                        log.debug("rolling back transaction [" + btm.getCurrentTransaction().getGtrid() + "]");
                        btm.rollback();
                    }
                    log.error("exception on receiving message and moving to errorStorage", e);
                } finally {
                    if (jdbcConnection != null) {
                        jdbcConnection.close();
                    }
                    if (jmsConnection != null) {
                        jmsConnection.stop();
                    }
                    if (jmsConsumer != null) {
                        jmsConsumer.close();
                    }
                    if (jmsSession != null) {
                        jmsSession.close();
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("exception on receiving message and moving to errorStorage", e);
    } finally {
        if (jmsConnection != null) {
            try {
                jmsConnection.close();
            } catch (JMSException e) {
                log.warn("exception on closing connection", e);
            }
        }
        if (jmsConnectionFactory != null) {
            jmsConnectionFactory.close();
        }
        if (jdbcDataSource != null) {
            jdbcDataSource.close();
        }
        if (btm != null) {
            btm.shutdown();
        }
    }
    return result;
}
Also used : PoolingDataSource(bitronix.tm.resource.jdbc.PoolingDataSource) MessageConsumer(javax.jms.MessageConsumer) Serializable(java.io.Serializable) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException) BitronixTransactionManager(bitronix.tm.BitronixTransactionManager) Date(java.util.Date) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) ListenerException(nl.nn.adapterframework.core.ListenerException) JmsException(nl.nn.adapterframework.jms.JmsException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) PoolingConnectionFactory(bitronix.tm.resource.jms.PoolingConnectionFactory) ListenerException(nl.nn.adapterframework.core.ListenerException) MessageWrapper(nl.nn.adapterframework.receivers.MessageWrapper) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 53 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class GenericMDB method onMessage.

public void onMessage(Message message) {
    try {
        // in locking
        if (this.listener == null) {
            this.listener = retrieveListener();
        }
        IMessageHandler handler = this.listener.getHandler();
        handler.processRawMessage(listener, message);
    } catch (ListenerException ex) {
        log.error(ex, ex);
        listener.getExceptionListener().exceptionThrown(listener, ex);
        rollbackTransaction();
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) IMessageHandler(nl.nn.adapterframework.core.IMessageHandler)

Example 54 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class ListenerPortPoller method toggleConfiguratorState.

/**
 * Toggle the state of the EjbListenerPortConnector instance by starting/stopping
 * the receiver it is attached to (via the JmsListener).
 * This method changes the state of the Receiver to match the state of the
 * WebSphere ListenerPort.
 *
 * @param elpc ListenerPortConnector for which state is to be changed.
 *
 * @throws nl.nn.adapterframework.configuration.ConfigurationException
 */
public void toggleConfiguratorState(IListenerConnector elpc) throws ConfigurationException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    GenericReceiver receiver = (GenericReceiver) getListener(elpc).getReceiver();
    if (isListenerPortClosed(elpc)) {
        if (receiver.isInRunState(RunStateEnum.STARTED)) {
            log.info("Stopping Receiver [" + receiver.getName() + "] because the WebSphere Listener-Port is in state 'stopped' but the JmsConnector in state 'open' and the receiver is in state 'started'");
            receiver.stopRunning();
        } else {
            log.info(// elpc.getListenerPortName(getListener(elpc))
            "ListenerPort [" + "] is in closed state, Listener is not in closed state, but Receiver is in state [" + receiver.getRunState().getName() + "] so the state of Receiver will not be changed.");
        }
    } else {
        // Only start the receiver for adapters which are running.
        if (receiver.getAdapter().getRunState().equals(RunStateEnum.STARTED)) {
            if (!receiver.isInRunState(RunStateEnum.STARTING)) {
                log.info("Starting Receiver [" + receiver.getName() + "] because the WebSphere Listener-Port is in state 'started' but the JmsConnector in state 'closed'");
                receiver.startRunning();
            } else {
                log.info("Receiver [" + receiver.getName() + "] still starting, so not changing anything now");
            }
        } else {
            try {
                log.warn("JmsConnector is closed, Adapter is not in state '" + RunStateEnum.STARTED + "', but WebSphere Jms Listener Port is in state 'started'. Stopping the listener port.");
                elpc.stop();
            } catch (ListenerException ex) {
                log.error(ex, ex);
            }
        }
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) GenericReceiver(nl.nn.adapterframework.receivers.GenericReceiver)

Example 55 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class RestListener method processRequest.

public String processRequest(String correlationId, String message, IPipeLineSession requestContext) throws ListenerException {
    HttpServletRequest httpServletRequest = (HttpServletRequest) requestContext.get(IPipeLineSession.HTTP_REQUEST_KEY);
    String response;
    String contentType = (String) requestContext.get("contentType");
    // Check if valid path
    String requestRestPath = (String) requestContext.get("restPath");
    if (!getRestPath().equals(requestRestPath)) {
        throw new ListenerException("illegal restPath value [" + requestRestPath + "], must be '" + getRestPath() + "'");
    }
    // Check if consumes has been set or contentType is set to JSON
    if (getConsumes().equalsIgnoreCase("JSON") && "application/json".equalsIgnoreCase(httpServletRequest.getContentType())) {
        try {
            message = transformToXml(message);
        } catch (PipeRunException e) {
            throw new ListenerException("Failed to transform JSON to XML");
        }
    }
    int eTag = 0;
    // Check if contentType is not overwritten which disabled auto-converting and mediatype headers
    if (contentType == null || StringUtils.isEmpty(contentType) || contentType.equalsIgnoreCase("*/*")) {
        if (getProduces().equalsIgnoreCase("XML"))
            requestContext.put("contentType", "application/xml");
        if (getProduces().equalsIgnoreCase("JSON"))
            requestContext.put("contentType", "application/json");
        if (getProduces().equalsIgnoreCase("TEXT"))
            requestContext.put("contentType", "text/plain");
        response = super.processRequest(correlationId, message, requestContext);
        if (response != null && !response.isEmpty())
            eTag = response.hashCode();
        if (getProduces().equalsIgnoreCase("JSON")) {
            try {
                response = transformToJson(response);
            } catch (PipeRunException e) {
                throw new ListenerException("Failed to transform XML to JSON");
            }
        }
    } else {
        response = super.processRequest(correlationId, message, requestContext);
        if (response != null && !response.isEmpty())
            eTag = response.hashCode();
    }
    if (!requestContext.containsKey("etag") && getGenerateEtag() && eTag != 0) {
        // The etag can be a negative integer...
        requestContext.put("etag", RestListenerUtils.formatEtag(getRestPath(), getUriPattern(), eTag));
    }
    return response;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ListenerException(nl.nn.adapterframework.core.ListenerException) PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Aggregations

ListenerException (nl.nn.adapterframework.core.ListenerException)84 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)37 IOException (java.io.IOException)19 HashMap (java.util.HashMap)18 Map (java.util.Map)18 JMSException (javax.jms.JMSException)16 SenderException (nl.nn.adapterframework.core.SenderException)14 SQLException (java.sql.SQLException)13 PreparedStatement (java.sql.PreparedStatement)11 Session (javax.jms.Session)10 LinkedHashMap (java.util.LinkedHashMap)9 IPipeLineSession (nl.nn.adapterframework.core.IPipeLineSession)9 TimeOutException (nl.nn.adapterframework.core.TimeOutException)9 IfsaException (nl.nn.adapterframework.extensions.ifsa.IfsaException)9 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 QueueSession (javax.jms.QueueSession)8 File (java.io.File)7 MessageConsumer (javax.jms.MessageConsumer)7 Message (javax.jms.Message)6