Apache Camel: Get move path after file transfer - java

I am creating a file transfer route which is using move to set a dynamic path where the file is moved after successful file transfer. I have also setup a notifier to keep track of file transfer events.
As the move path is dynamic, I need to get the evaluated path where file was moved after the file transfer. How can this path inside the notifier?
public class MyFtpServiceBuilder extends RouteBuilder {
#Override
public void configure() throws Exception {
getContext()
.getManagementStrategy()
.addEventNotifier(new MyFtpServiceNotifier());
from("file:C:/tmp/inputfolder?move=archive/${date:now:yyyyMMdd}/${file:onlyname}")
.routeId("myRoute")
.to("file:C:/tmp/outputfolder")
}
}
public class MyFtpServiceNotifier extends EventNotifierSupport {
#Override
public void notify(EventObject event) throws Exception {
Exchange exchange = ((AbstractExchangeEvent) event).getExchange();
if (event instanceof ExchangeSentEvent) {
// Want to get here the path where file was moved
}
}
#Override
public boolean isEnabled(EventObject event) {
return event instanceof AbstractExchangeEvent;
}
}

You could try to add a header to the exchange, something like:
.setHeader("FILE_PROCESSED").simple("archive/${date:now:yyyyMMdd}/${file:onlyname}")

I'm not sure how to do it in a notifier, but you can always add a Processor and use the FileEndpoint.getMove().evaluate() method on the exchange to obtain the final moved file path.
For example:
#Override
public void process(final Exchange exchange) throws Exception {
File movedFile = null;
if (exchange.getFromEndpoint() instanceof FileEndpoint) {
FileEndpoint fileEndpoint = (FileEndpoint) exchange.getFromEndpoint();
String movePath = fileEndpoint.getMove().evaluate(exchange, String.class);
File inputDir = fileEndpoint.getFile();
movedFile = new File(inputDir, movePath);
}
}

Related

pass context to callable

I am creating several tasks to download some files from the internet and I want to access the context in MyTasks.class . What is the best way?
public class MyTasks {
public Callable<File> DownloadImage(String url) {
return new Callable<File>() {
#Override
public File call() throws Exception {
File imageFile = new File(- context here - .getExternalFilesDir(null) , "example.png");
// download task here
return imageFile;
}
};
}
}
If I pass context via constructor will it cause memory leak or any other problems ?

How to dynamically configure log directory (every request) in java util logging with helidon

I want to configure separate log directory for every request. Is this possible with helidon?
The oracle helidon JUL examples are located in github. Building off of those examples you would have to create a custom Handler to read the request from the HelidonMdc:
import io.helidon.logging.jul;
import io.helidon.logging.common;
import java.util.logging.*;
public class RequestFileHandler extends Handler {
public RequestFileHandler() {
super.setFormatter(new HelidonFormatter());
}
#Override
public synchronized void publish(LogRecord r) {
if (isLoggable(r)) {
try {
FileHandler h = new FileHandler(fileName(r), Integer.MAX_VALUE, 1, true);
try {
h.setLevel(getLevel());
h.setEncoding(getEncoding());
h.setFilter(null);
h.setFormatter(getFormatter());
h.setErrorManager(getErrorManager());
h.publish(r);
} finally {
h.close();
}
} catch (IOException | SecurityException jm) {
this.reportError(null, jm, ErrorManager.WRITE_FAILURE);
}
}
}
#Override
public void flush() {
}
#Override
public void close() {
super.setLevel(Level.OFF);
}
private String fileName(LogRecord r) {
Optional<String> o = HelidonMdc.get("name");
return o.isPresent() ? o.get() +".log" : "unknown.log";
}}
Like the example code this code is assuming that you have set the value of 'name' to the request id. You would then have to install this handler on your application logger.

Adding new resources dynamically - CoAP

Once CoAP server is started I need to add new resources dynamically. But I have to stop and start the server again in order to access new resources. I suppose adding new resources same as adding a new HTTP servlet into already started HTTP server.
Here I added source code which is used for adding dynamic resources. If I am missing anything here let me know.
private static CoapServer server;
public CoAPEventAdapter(InputEventAdapterConfiguration eventAdapterConfiguration,
Map<String, String> globalProperties) {
this.eventAdapterConfiguration = eventAdapterConfiguration;
this.globalProperties = globalProperties;
if(server == null){
server = new CoapServer();
server.start();
}
}
#Override
public void connect() {
registerDynamicEndpoint(eventAdapterConfiguration.getName());
isConnected = true;
}
private void registerDynamicEndpoint(String adapterName) {
server.stop();
server.add(new HelloWorldResource(adapterName));
server.start();
}
class HelloWorldResource extends CoapResource {
public HelloWorldResource(String resourceName) {
// set resource identifier
super(resourceName);
// set display name
getAttributes().setTitle("Hello-World Resource");
}
#Override
public void handleGET(CoapExchange exchange) {
// respond to the request
exchange.respond("Hello World!");
}
}
I've just tried to add a CoapResource dynamically:
#Override
public void handleGET(CoapExchange exchange) {
server.getRoot().add(new CoapResource("dynstatus") {
#Override
public void handleGET(CoapExchange exchange) {
System.err.println("dynstatus!!!");
}
});
And was able to call it.

How to keep initial log file in rotating log handler

I am using Java logging to log the memory static in my file and use java.util.logging.FileHandler to implement rotating log. Now I have a situation where my manager wants to keep the initial logging file and rotate the rest of the file. Is there any way I can keep the initial log file but yet rotate the rest of the file.
public class TopProcessor extends Handler {
Handler handler;
public TopProcessor() throws IOException{
File dir = new File(System.getProperty("user.home"), "logs");
dir.mkdirs();
File fileDir = new File(dir,"metrics");
fileDir.mkdirs();
String pattern = "metrics-log-%g.json";
int count = 5;
int limit = 500000;
handler = new TopProcessorHandler(fileDir.getAbsolutePath()+File.separator+pattern, limit, count);
}
class TopProcessorHandler extends FileHandler{
public TopProcessorHandler(String pattern, int limit, int count)
throws IOException {
super(pattern, limit, count);
}
}
private void writeInformationToFile(String information) {
handler.publish(new LogRecord(Level.ALL, information));
}
#Override
public void close() {
handler.close();
}
#Override
public void flush() {
handler.flush();
}
#Override
public void publish(LogRecord record) {
handler.publish(record);
}
}
Create 2 files one initial log file and other rotating log file..You can merge two files when you want to read logs

Camel file unit test

I am new to Apache Camel, I have written a simple route to scan a directory (/test), file will be processed when it was copied into the directory. Anyone has an idea on how to write a camel unit test to test the following route? Is there a way to mock the process of copying the file into the /test directory so that the route will be triggered.
public void configure() {
from( "file:/test?preMove=IN_PROGRESS" +
"&move=completed/${date:now:yyyyMMdd}/${file:name}" +
"&moveFailed=FAILED/${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.${file:ext}" )
.process(new Processor() {
public void process(Exchange exchange) throws IOException {
File file = (File) exchange.getIn().getBody();
// read file content ......
}
});
}
You have done the routing by one of many correct ways. But there exist some more important pieces to make your code run - you should create a context, create a router with this your configure(), add it to a context, and run this context.
Sorry, I prefer beans to processors, so you have also to register a bean. And make you processing a normal named method in a named class.
I think, the most compact info is here. JUnit test is a standalone app and you need to run Camel as a standalone app for JUnit testing.
I think the basic idea is that you mock the end endpoint so you can check what is coming out your route. There are a few different ways, but you could test your route as follows:
public class MyRouteTest extends CamelSpringTestSupport {
private static final String INPUT_FILE = "myInputFile.xml";
private static final String URI_START = "direct:start";
private static final String URI_END = "mock:end";
#Override
public boolean isUseAdviceWith() {
return true;
}
#Override
protected AbstractApplicationContext createApplicationContext() {
return new AnnotationConfigApplicationContext(CamelTestConfig.class); // this is my Spring test config, where you wire beans
}
#Override
protected RouteBuilder createRouteBuilder() {
MyRoute route = new MyRoute();
route.setFrom(URI_START); // I have added getter and setters to MyRoute so I can mock 'start' and 'end'
route.setTo(URI_END);
return route;
}
#Test
public void testMyRoute() throws Exception {
MockEndpoint result = getMockEndpoint(URI_END);
context.start();
// I am just checking I receive 5 messages, but you should actually check the content with expectedBodiesReceived() depending on what your processor does to the those files.
result.expectedMessageCount(5);
// I am just sending the same file 5 times
for (int i = 0; i < 5; i++) {
template.sendBody(URI_START, getInputFile(INPUT_FILE));
}
result.assertIsSatisfied();
context.stop();
}
private File getInputFile(String name) throws URISyntaxException, IOException {
return FileUtils.getFile("src", "test", "resources", name);
}
I am sure you already solved your issue is 2013, but this is how I would solve it in 2017. Regards

Categories

Resources