Search in sources :

Example 11 with ProduceException

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

the class FsMessageProducerTest method testProduceFileAlreadyExists_NioWorker.

@Test
public void testProduceFileAlreadyExists_NioWorker() throws Exception {
    String subdir = new GuidGenerator().safeUUID();
    FsProducer fs = createProducer(subdir);
    fs.setCreateDirs(true);
    fs.setFsWorker(new NioWorker());
    fs.setFilenameCreator(new MetadataFileNameCreator("targetFilename"));
    File parentDir = FsHelper.createFileReference(FsHelper.createUrlFromString(PROPERTIES.getProperty(BASE_KEY), true));
    try {
        File dir = new File(parentDir, subdir);
        start(fs);
        AdaptrisMessage msg = new DefaultMessageFactory().newMessage(TEXT);
        msg.addMetadata("targetFilename", new GuidGenerator().safeUUID());
        dir.mkdirs();
        File targetFile = new File(dir, msg.getMetadataValue("targetFilename"));
        targetFile.createNewFile();
        fs.produce(msg);
        fail();
    } catch (ProduceException expected) {
    } finally {
        FileUtils.deleteQuietly(new File(parentDir, subdir));
        stop(fs);
    }
}
Also used : DefaultMessageFactory(com.adaptris.core.DefaultMessageFactory) NioWorker(com.adaptris.fs.NioWorker) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) GuidGenerator(com.adaptris.util.GuidGenerator) File(java.io.File) MetadataFileNameCreator(com.adaptris.core.MetadataFileNameCreator) ProduceException(com.adaptris.core.ProduceException) Test(org.junit.Test)

Example 12 with ProduceException

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

the class FsProducer method doProduce.

@Override
@SuppressWarnings({ "lgtm[java/path-injection]" })
protected void doProduce(AdaptrisMessage msg, String baseUrl) throws ProduceException {
    FileNameCreator creator = filenameCreatorToUse();
    try {
        URL url = FsHelper.createUrlFromString(baseUrl, true);
        validateDir(url);
        File filetoWrite = new File(FsHelper.createFileReference(url), creator.createName(msg));
        addProducerMetadata(msg, filetoWrite);
        write(msg, filetoWrite);
        log.debug("msg produced to destination [{}]", url);
    } catch (Exception e) {
        throw new ProduceException(e);
    }
}
Also used : FileNameCreator(com.adaptris.core.FileNameCreator) File(java.io.File) URL(java.net.URL) ProduceException(com.adaptris.core.ProduceException) IOException(java.io.IOException) CoreException(com.adaptris.core.CoreException) ProduceException(com.adaptris.core.ProduceException)

Example 13 with ProduceException

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

the class RelaxedFtpProducer method doProduce.

/**
 * @see com.adaptris.core.AdaptrisMessageProducerImp#produce(AdaptrisMessage)
 */
@Override
public void doProduce(AdaptrisMessage msg, String endpoint) throws ProduceException {
    FileTransferConnection conn = retrieveConnection(FileTransferConnection.class);
    FileTransferClient client = null;
    try {
        client = conn.connect(endpoint);
        String dirRoot = conn.getDirectoryRoot(endpoint);
        String fileName = filenameCreator().createName(msg);
        String destFilename = dirRoot + SLASH + fileName;
        if (dirRoot.endsWith(SLASH)) {
            destFilename = dirRoot + fileName;
        }
        log.debug("destFilename=[{}]", destFilename);
        msg.addMetadata(CoreConstants.PRODUCED_NAME_KEY, fileName);
        if (getEncoder() != null) {
            byte[] bytesToWrite = encode(msg);
            client.put(bytesToWrite, destFilename);
        } else {
            try (InputStream in = msg.getInputStream()) {
                client.put(in, destFilename);
            }
        }
    } catch (Exception e) {
        throw new ProduceException(e);
    } finally {
        conn.disconnect(client);
    }
}
Also used : FileTransferClient(com.adaptris.filetransfer.FileTransferClient) InputStream(java.io.InputStream) ProduceException(com.adaptris.core.ProduceException) CoreException(com.adaptris.core.CoreException) ProduceException(com.adaptris.core.ProduceException)

Example 14 with ProduceException

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

the class ActiveMqJmsTransactedWorkflowTest method testRuntimeException.

@Test
public void testRuntimeException() throws Exception {
    int msgCount = 10;
    String destination = createSafeUniqueId(new Object());
    Channel channel = createStartableChannel(activeMqBroker, true, "testRuntimeException", destination);
    JmsTransactedWorkflow workflow = (JmsTransactedWorkflow) channel.getWorkflowList().get(0);
    workflow.setProducer(new MockMessageProducer() {

        @Override
        protected void doProduce(AdaptrisMessage msg, String endpoint) throws ProduceException {
            throw new RuntimeException();
        }
    });
    try {
        channel.requestStart();
        StandaloneProducer sender = new StandaloneProducer(activeMqBroker.getJmsConnection(), new PtpProducer().withQueue((destination)));
        send(sender, msgCount);
    } finally {
        channel.requestClose();
    }
    assertEquals(msgCount, activeMqBroker.messagesOnQueue(destination));
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MockChannel(com.adaptris.core.stubs.MockChannel) Channel(com.adaptris.core.Channel) JmsTransactedWorkflow(com.adaptris.core.jms.JmsTransactedWorkflow) PtpProducer(com.adaptris.core.jms.PtpProducer) StandaloneProducer(com.adaptris.core.StandaloneProducer) ProduceException(com.adaptris.core.ProduceException) Test(org.junit.Test)

Example 15 with ProduceException

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

the class ActiveMqJmsTransactedWorkflowTest method testProduceException.

@Test
public void testProduceException() throws Exception {
    int msgCount = 10;
    String destination = createSafeUniqueId(new Object());
    Channel channel = createStartableChannel(activeMqBroker, true, "testProduceException", destination);
    JmsTransactedWorkflow workflow = (JmsTransactedWorkflow) channel.getWorkflowList().get(0);
    workflow.setProducer(new MockMessageProducer() {

        @Override
        protected void doProduce(AdaptrisMessage msg, String endpoint) throws ProduceException {
            throw new ProduceException();
        }
    });
    try {
        channel.requestStart();
        StandaloneProducer sender = new StandaloneProducer(activeMqBroker.getJmsConnection(), new PtpProducer().withQueue((destination)));
        send(sender, msgCount);
    } finally {
        channel.requestClose();
    }
    assertEquals(msgCount, activeMqBroker.messagesOnQueue(destination));
}
Also used : MockMessageProducer(com.adaptris.core.stubs.MockMessageProducer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MockChannel(com.adaptris.core.stubs.MockChannel) Channel(com.adaptris.core.Channel) JmsTransactedWorkflow(com.adaptris.core.jms.JmsTransactedWorkflow) PtpProducer(com.adaptris.core.jms.PtpProducer) StandaloneProducer(com.adaptris.core.StandaloneProducer) ProduceException(com.adaptris.core.ProduceException) Test(org.junit.Test)

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