apache camel 案例,包含与 mysql,activeMQ等的使用案例,可以看下,简单易懂!!!
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.22.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-camel -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
<version>5.15.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jdbc -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
<version>2.22.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
package top.daocaor.example1;
import org.apache.camel.builder.RouteBuilder;
public class HelloWorldRoute extends RouteBuilder {
public void configure() throws Exception {
System.out.println("hello world camel");
}
}
package top.daocaor.example1;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
public class HelloWorld {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new HelloWorldRoute());
context.start();
}
}
package top.daocaor.example2;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
public class FileCopy {
public static void main(String[] args) throws Exception {
CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file:input_box?noop=true")
.to("file:output_box");
}
});
while (true)
camelContext.start();
}
}
package top.daocaor.example3;
import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
public class ProAndConsumeExample {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
//System.out.println(" I am the processor ... ");
String message = exchange.getIn().getBody(String.class);
message = message + "-by daocaor";
exchange.getOut().setBody(message);
}
})
.to("seda:end");
}
});
context.start();
ProducerTemplate producerTemplate = context.createProducerTemplate();
producerTemplate.sendBody("direct:start","Hello Everyone");
ConsumerTemplate consumerTemplate = context.createConsumerTemplate();
String message = consumerTemplate.receiveBody("seda:end",String.class);
System.out.println(message);
}
}
package top.daocaor.example4;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;
import javax.jms.ConnectionFactory;
public class ActiveMQConsume {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
context.addComponent("jms", JmsComponent.jmsComponentClientAcknowledge(connectionFactory));
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("activemq:queue:my_queue")
.to("seda:end");
}
});
context.start();
ConsumerTemplate consumerTemplate = context.createConsumerTemplate();
String message = consumerTemplate.receiveBody("seda:end",String.class);
System.out.println(message);
}
}
package top.daocaor.example4;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;
import javax.jms.ConnectionFactory;
public class FileToActiveMQ {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
context.addComponent("jms", JmsComponent.jmsComponentClientAcknowledge(connectionFactory));
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file:input_box?noop=true")
.to("activemq:queue:my_queue");
}
});
while (true)
context.start();
}
}
package top.daocaor.example4;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;
import javax.jms.ConnectionFactory;
import java.util.Date;
public class ObjectToActiveMQ {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
context.addComponent("jms", JmsComponent.jmsComponentClientAcknowledge(connectionFactory));
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to("activemq:queue:my_queue");
}
});
context.start();
ProducerTemplate producerTemplate = context.createProducerTemplate();
producerTemplate.sendBody("direct:start","246307");
}
}
package top.daocaor.example5;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
public class CallMethodUsingClassComponent {
public static void main(String[] args) throws Exception {
// CamelContext context = new DefaultCamelContext();
//
//
// context.addRoutes(new RouteBuilder() {
// @Override
// public void configure() throws Exception {
// from("direct:start")
// .to("class:top.daocaor.example5.MyService?method=doSomething");
// }
// });
//
// context.start();
//
// ProducerTemplate producerTemplate = context.createProducerTemplate();
// producerTemplate.sendBody("direct:start","Hello");
MyService myService = new MyService();
SimpleRegistry registry = new SimpleRegistry();
registry.put("myService",myService);
CamelContext context = new DefaultCamelContext(registry);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to("bean:myService?method=doSomething");
}
});
context.start();
ProducerTemplate producerTemplate = context.createProducerTemplate();
producerTemplate.sendBody("direct:start","Hello");
}
}
package top.daocaor.example5;
public class MyService {
public void doSomething(String message){
System.out.println(message);
}
}
package top.daocaor.example6;
import java.util.List;
public class ResultHandler {
public void printResult(List list){
for(int i = 0 ; i < list.size() ; ++ i ){
System.out.println(list.get(i));
}
}
}
package top.daocaor.example6;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
public class SQLOperation {
public static void main(String[] args) throws Exception {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/camel_tutorial");
dataSource.setUser("root");
dataSource.setPassword("root");
SimpleRegistry registry = new SimpleRegistry();
registry.put("myDataSource",dataSource);
CamelContext context = new DefaultCamelContext(registry);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to("jdbc:myDataSource")
.bean(new ResultHandler(),"printResult");
}
});
context.start();
ProducerTemplate producerTemplate = context.createProducerTemplate();
producerTemplate.sendBody("direct:start","select * from tableName");
}
}