Search in sources :

Example 21 with ProduceException

use of com.adaptris.core.ProduceException in project interlok by adaptris.

the class MessageMetricsInterceptorTest method testRestartProducerAfterProduceFailure.

@Test
public void testRestartProducerAfterProduceFailure() throws Exception {
    doThrow(new ProduceException("Expected.")).when(mockStandaloneProducer).produce(any());
    ProducingStatisticManager producingStatisticManager = new ProducingStatisticManager();
    producingStatisticManager.setMarshaller(mockMarshaller);
    producingStatisticManager.setProducer(mockStandaloneProducer);
    metricsInterceptor.setStatisticManager(producingStatisticManager);
    LifecycleHelper.init(metricsInterceptor);
    LifecycleHelper.start(metricsInterceptor);
    AdaptrisMessage message = DefaultMessageFactory.getDefaultInstance().newMessage();
    // A minus time will expire the time slice immediately after the first message
    metricsInterceptor.setTimesliceDuration(new TimeInterval(-1L, TimeUnit.SECONDS));
    assertEquals(0, metricsInterceptor.getStats().size());
    submitMessage(message);
    assertEquals(1, metricsInterceptor.getStats().size());
    submitMessage(message);
    verify(mockMarshaller).marshal(any());
    verify(mockStandaloneProducer).produce(any());
    // test the restart.
    verify(mockStandaloneProducer).requestStop();
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ProduceException(com.adaptris.core.ProduceException) Test(org.junit.Test)

Example 22 with ProduceException

use of com.adaptris.core.ProduceException in project interlok by adaptris.

the class SimpleHttpProducer method doRequest.

@Override
protected AdaptrisMessage doRequest(AdaptrisMessage msg, String destination, long timeout) throws ProduceException {
    AdaptrisMessage reply = defaultIfNull(getMessageFactory()).newMessage();
    OutputStream out = null;
    try {
        HttpClientConnection c = retrieveConnection(HttpClientConnection.class);
        HttpClientTransport client = c.initialiseClient(destination);
        client.setMethod(getMethod());
        HttpMessage m = HttpMessageFactory.getDefaultInstance().create();
        applyHeaders(getAdditionalHeaders(msg), m);
        if (getContentTypeKey() != null && msg.containsKey(getContentTypeKey())) {
            m.getHeaders().put(Http.CONTENT_TYPE, msg.getMetadataValue(getContentTypeKey()));
        }
        if (getAuthorisation() != null) {
            m.getHeaders().put(Http.AUTHORIZATION, getAuthorisation());
        }
        if (getEncoder() != null) {
            getEncoder().writeMessage(msg, m);
        } else {
            out = m.getOutputStream();
            out.write(msg.getPayload());
            out.flush();
        }
        HttpSession httpSession = client.send(m, new Long(timeout).intValue(), handleRedirection());
        readResponse(httpSession, reply);
        httpSession.close();
    } catch (HttpException e) {
        throw new ProduceException(e);
    } catch (IOException e) {
        throw new ProduceException(e);
    } catch (CoreException e) {
        throw new ProduceException(e);
    }
    return reply;
}
Also used : HttpClientTransport(com.adaptris.http.HttpClientTransport) CoreException(com.adaptris.core.CoreException) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) HttpSession(com.adaptris.http.HttpSession) OutputStream(java.io.OutputStream) HttpException(com.adaptris.http.HttpException) IOException(java.io.IOException) HttpMessage(com.adaptris.http.HttpMessage) ProduceException(com.adaptris.core.ProduceException)

Example 23 with ProduceException

use of com.adaptris.core.ProduceException in project interlok by adaptris.

the class JmsProducer method request.

public AdaptrisMessage request(AdaptrisMessage msg, String dest, long timeout) throws ProduceException {
    AdaptrisMessage translatedReply = defaultIfNull(getMessageFactory()).newMessage();
    Destination replyTo = null;
    MessageConsumer receiver = null;
    try {
        setupSession(msg);
        JmsDestination target = buildDestination(dest, msg, true);
        replyTo = target.getReplyToDestination();
        // Listen for the reply.
        receiver = currentSession().createConsumer(replyTo);
        produce(msg, target);
        translatedReply = waitForReply(receiver, timeout);
        // BUG#915
        commit();
    } catch (Exception e) {
        logLinkedException("", e);
        rollback();
        throw ExceptionHelper.wrapProduceException(e);
    } finally {
        JmsUtils.closeQuietly(receiver);
        JmsUtils.deleteTemporaryDestination(replyTo);
    }
    return mergeReply(translatedReply, msg);
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ProduceException(com.adaptris.core.ProduceException) JMSException(javax.jms.JMSException) CoreException(com.adaptris.core.CoreException)

Example 24 with ProduceException

use of com.adaptris.core.ProduceException in project interlok by adaptris.

the class DefinedJmsProducer method doProduce.

protected void doProduce(AdaptrisMessage msg, String dest, Destination replyTo) throws ProduceException {
    Destination jmsDest = null;
    try {
        Args.notNull(dest, "destination");
        // First of all directly try to get a Destination object if available.
        jmsDest = retrieveObjectDestination(dest, msg);
        if (jmsDest == null) {
            jmsDest = createDestination(dest);
        }
        Args.notNull(jmsDest, "destination");
        doProduce(msg, jmsDest, replyTo);
        commit();
    } catch (Exception e) {
        log.warn("Error producing to destination [{}]", dest);
        logLinkedException("Produce", e);
        rollback();
        throw ExceptionHelper.wrapProduceException(e);
    }
}
Also used : Destination(javax.jms.Destination) CoreException(com.adaptris.core.CoreException) ProduceException(com.adaptris.core.ProduceException) JMSException(javax.jms.JMSException)

Example 25 with ProduceException

use of com.adaptris.core.ProduceException in project interlok by adaptris.

the class DefinedJmsProducer method request.

public AdaptrisMessage request(AdaptrisMessage msg, String dest, long timeout) throws ProduceException {
    AdaptrisMessage translatedReply = defaultIfNull(getMessageFactory()).newMessage();
    Destination replyTo = null;
    MessageConsumer receiver = null;
    try {
        setupSession(msg);
        getMessageTranslator().registerSession(producerSession().getSession());
        if (msg.headersContainsKey(JMS_ASYNC_STATIC_REPLY_TO)) {
            replyTo = createDestination(msg.getMetadataValue(JMS_ASYNC_STATIC_REPLY_TO));
        } else {
            replyTo = createTemporaryDestination();
        }
        receiver = currentSession().createConsumer(replyTo);
        doProduce(msg, dest, replyTo);
        Message jmsReply = receiver.receive(timeout);
        translatedReply = Optional.ofNullable(MessageTypeTranslatorImp.translate(getMessageTranslator(), jmsReply)).orElseThrow(() -> new JMSException("No Reply Received within " + timeout + "ms"));
        acknowledge(jmsReply);
        // BUG#915
        commit();
    } catch (Exception e) {
        logLinkedException("", e);
        rollback();
        throw new ProduceException(e);
    } finally {
        JmsUtils.closeQuietly(receiver);
        JmsUtils.deleteTemporaryDestination(replyTo);
    }
    return mergeReply(translatedReply, msg);
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) Message(javax.jms.Message) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) JMSException(javax.jms.JMSException) CoreException(com.adaptris.core.CoreException) ProduceException(com.adaptris.core.ProduceException) JMSException(javax.jms.JMSException) ProduceException(com.adaptris.core.ProduceException)

Aggregations

ProduceException (com.adaptris.core.ProduceException)29 Test (org.junit.Test)17 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)13 CoreException (com.adaptris.core.CoreException)9 MockMessageProducer (com.adaptris.core.stubs.MockMessageProducer)7 Channel (com.adaptris.core.Channel)6 StandaloneProducer (com.adaptris.core.StandaloneProducer)6 IOException (java.io.IOException)6 DefaultMessageFactory (com.adaptris.core.DefaultMessageFactory)5 File (java.io.File)5 GuidGenerator (com.adaptris.util.GuidGenerator)4 JMSException (javax.jms.JMSException)4 PtpProducer (com.adaptris.core.jms.PtpProducer)3 MockChannel (com.adaptris.core.stubs.MockChannel)3 InputStream (java.io.InputStream)3 Destination (javax.jms.Destination)3 FileNameCreator (com.adaptris.core.FileNameCreator)2 MetadataElement (com.adaptris.core.MetadataElement)2 MetadataFileNameCreator (com.adaptris.core.MetadataFileNameCreator)2 ServiceException (com.adaptris.core.ServiceException)2