Camel custom component example

Source: Internet
Author: User

Camel custom component example
To customize components in Camel, we need to understand the key concepts in Camel, understand the routing construction and startup processes in Camel, and have related descriptions in the previous article.

Here is an example of a custom component. This example is still subject to file polling. The following is the specific code.

Component class:

package com.xtayfjpk.esb.components.file;import java.util.Map;import org.apache.camel.Endpoint;import org.apache.camel.impl.DefaultComponent;public class MyFileComponent extends DefaultComponent {@Overrideprotected Endpoint createEndpoint(String uri, String remaining, Map
 
   parameters) throws Exception {return new MyFileEndpoint(this, uri);}}
 

Endpoint class:

package com.xtayfjpk.esb.components.file;import java.io.File;import org.apache.camel.Consumer;import org.apache.camel.Exchange;import org.apache.camel.Processor;import org.apache.camel.Producer;import org.apache.camel.component.file.FileComponent;import org.apache.camel.impl.DefaultEndpoint;import org.apache.camel.impl.DefaultExchange;public class MyFileEndpoint extends DefaultEndpoint {public MyFileEndpoint(MyFileComponent component, String uri) {super(uri, component);}@Overridepublic Producer createProducer() throws Exception {return new MyFileProducer(this);}@Overridepublic Consumer createConsumer(Processor processor) throws Exception {return new MyFileConsumer(this, processor);}@Overridepublic boolean isSingleton() {return false;}public Exchange createExchange(File file) {Exchange exchange = new DefaultExchange(getCamelContext());exchange.setProperty(FileComponent.FILE_EXCHANGE_FILE, file);exchange.getIn().setBody(file, File.class);return exchange;}}

Consumer class:

package com.xtayfjpk.esb.components.file;import java.io.File;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;import org.apache.camel.Endpoint;import org.apache.camel.Exchange;import org.apache.camel.Processor;import org.apache.camel.impl.DefaultConsumer;public class MyFileConsumer extends DefaultConsumer implements Runnable {private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);private File pollDir;public MyFileConsumer(Endpoint endpoint, Processor processor) {super(endpoint, processor);String pollDir = endpoint.getEndpointUri().substring(endpoint.getEndpointUri().indexOf(":")+1);this.pollDir = new File(pollDir);}@Overrideprotected void doStart() throws Exception {super.doStart();executorService.scheduleAtFixedRate(this, 1000L, 1000L, TimeUnit.MILLISECONDS);}@Overridepublic void run() {File[] files = pollDir.listFiles();for(File file : files) {MyFileEndpoint endpoint = (MyFileEndpoint) getEndpoint();Exchange exchange = endpoint.createExchange(file);try {processExchange(exchange);} catch (Exception e) {e.printStackTrace();}}}private void processExchange(Exchange exchange) throws Exception {this.getProcessor().process(exchange);}}

Producer class:

package com.xtayfjpk.esb.components.file;import java.io.File;import org.apache.camel.Endpoint;import org.apache.camel.Exchange;import org.apache.camel.impl.DefaultProducer;import org.apache.commons.io.FileUtils;public class MyFileProducer extends DefaultProducer {private File outputDir;public MyFileProducer(Endpoint endpoint) {super(endpoint);this.outputDir = new File(endpoint.getEndpointUri().substring(endpoint.getEndpointUri().indexOf(":")+1));}@Overridepublic void process(Exchange exchange) throws Exception {File file = exchange.getIn().getBody(File.class);if(file!=null) {FileUtils.moveFileToDirectory(file, outputDir, true);}}}

Test class:

Package com. xforwarfjpk. esb. components. file; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. inputStreamReader; import java. io. printStream; import org. apache. camel. camelContext; import org. apache. camel. exchange; import org. apache. camel. processor; import org. apache. camel. builder. routeBuilder; import org. apache. camel. impl. defaultCamelContext; public class MyCompon EntTest {/*** @ param args * @ throws Exception */public static void main (String [] args) throws Exception {CamelContext camelContext = new DefaultCamelContext (); // For simplicity, this method is used to replace camelContext by placing the properties file under the class path META-INF/services/org/apache/camel/component. addComponent ("myfile", new MyFileComponent (); camelContext. addRoutes (new RouteBuilder () {@ Overridepublic void configure () throws Exception {this. From ("myfile: H:/temp/in "). process (new Processor () {@ Overridepublic void process (Exchange exchange) throws Exception {File file = exchange. getIn (). getBody (File. class); PrintStream ps = new PrintStream (System. out); BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (file); String line = null; while (line = br. readLine ())! = Null) {ps. println (line);} ps. close (); br. close ();}}). to ("myfile: H:/temp/out") ;}}); camelContext. start (); Object object = new Object (); synchronized (object) {object. wait ();}}}


If a component is a starting component or a source component, the corresponding Endpoint must be able to create a Consumer object. If the component is still a target component, then the corresponding Endpoint must be able to create a Producer object. Consumer is responsible for the generation of Exchange objects, while Producer is responsible for the consumption of Exchange objects. Of course, in reality, components cannot be written so easily, but the general process is the same. In this example, you can simply poll the components and move them to the specified directory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.