7 Ağustos 2020 Cuma

Camel RouteBuilder Sınıfı

Giriş
Şus satırı dahil ederiz
import org.apache.camel.builder.RouteBuilder;
configure metodu
Route kuralları DSL ile yazılır. from ve to ile hangi Component'ten hangi Component'e bilgi akacağı belirtilir. 

Açıklaması şöyle. DSL farklı diller ile yazılabilir. Sanırım en kolayı Fluent API
Apache Camel offers various DSLs, such as Java-based Fluent API, Spring, or Blueprint XML Configuration files, and a Scala DSL. 
- Spring XML DSL is based on the Spring framework and uses XML configuration. 
- Java DSL has the best IDE support.  
- Groovy and Scala DSLs are similar to the Java DSL; in addition, they offer the typical features of modern JVM languages, such as concise code or closures.
Örnek - JMS
Şöyle yaparız
@Component
public class SomeRoute extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    from("jms:{{queue.name}}")
    .process("xmlToJsonProcessor")
    .to("kafka:{{topic}}?brokers={{spring.kafka.bootstrap-servers}}&
      securityProtocol={{spring.kafka.properties.security.protocol}}&
      saslMechanism={{spring.kafka.properties.sasl.mechanism}}&
      saslJaasConfig={{spring.kafka.properties.sasl.jaas.config}}");
  }   
}
Eğer consumer sayısını artırmak istersek şöyle yaparız
@Component
public class SomeRoute extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    from("jms:{{queue.name}}?concurrentConsumers=10")
    .process("xmlToJsonProcessor")
    .to("...");
   }   
}
Örnek - netty
Şöyle yaparız
from("netty:tcp://localhost:9011?textline=true&encoding=ISO-8859-1&sync=true")
.process(new Processor() {
  @Override
  public void process(Exchange exchange) throws InterruptedException {
    String data=exchange.getIn().getBody(String.class);
    ...
  }
})
.to("log:?level=INFO&showBody=true");
Örnek - timer singleshot
Şöyle yaparız
from("timer://runOnce?repeatCount=1&delay=5000")
  .serviceCall("myService");
Örnek - timer periodic
Şöyle yaparız
from("timer://foo?period=100")
.to("direct:bar");
Örnek - timer periodic
Şöyle yaparız
from("timer:theTimer?period=10s")
.log("Timer Invoked . . . ")
.pollEnrich("file:data/input?delete=true&readLock=none")
.log("BODY = [ ${body} ] " )
.to("file:data/output");

Camel FileComponent

choice
choice + when + otherwise şeklinde kullanılır

Örnek
Şöyle yaparız
from("file:src/data?noop=true")
  .choice()
    .when(xpath("/person/city = 'London'"))
      .log("UK message")
      .to("file:target/messages/uk")
    .otherwise()
      .log("Other message")
      .to("file:target/messages/others");
Kullanım Örnekleri
Örnek - Dosya Kopyalamak
Şöyle yaparız

Örnek - Dosya Taşır
Şöyle yaparız.
public class SimpleRouteBuilder extends RouteBuilder {

  public static final String SOURCE_FOLDER = "/home/vikas/dev/camel/01/source";
  public static final String SOURCE_FOLDER = "/home/vikas/dev/camel/01/target";

  @Override
  public void configure() throws Exception {
    from("file:" + SOURCE_FOLDER).to("file:" + TARGET_FOLDER);
  }
}
Örnek
Şöyle yaparız
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class ActiveMQSenderRoute extends RouteBuilder {
  @Override
  public void configure() throws Exception {        
    from("file:files/input")
    .to("activemq:myqueue");
  }
}
charSet Alanı
Örnek
Şöyle yaparız
from("file://target/input?charset=UTF-8")
.process(new Processor() {
  public void process(Exchange msg) {
    String text = msg.getIn().getBody(String.class);
    LOG.info("Processing text: " + text);
  }
});
delay Alanı
FilePoller olarak kullanmak içindir.

Örnek
Şöyle yaparız
from("file://target/input?delay=1000")
.process(new Processor() {
  public void process(Exchange msg) {
    File file = msg.getIn().getBody(File.class);
    LOG.info("Processing file: " + file);
  }
});
Açıklaması şöyle
By default, the file component will create a org.apache.camel.component.file.GenericFile object for each file found and pass it down your Route as message body. You may retrieve all your file information through this object. Alternatively, you may also use the Exchange API to auto convert the message body object to a type you expect to receive (eg: as with msg.getIn().getBody(File.class)). In the example above, the File is a type you expect to get from the message body, and Camel will try to convert it for you. Camel uses the context’s registry space to pre-register many TypeConverter's that can handle the conversion of most of the common data types (like Java primitives). These TypeConverters are a powerful way to make your Route and Processor more flexible and portable.
delete Alanı
Açıklaması şöyle
There many configurable options from the file component. .... Some of these default behaviors are such that if the input folder doesn’t exist, it will create it. And when the file is done processing by the Route, it will be moved into a .camel folder. If you don’t want the file at all after processing, then set delete=true in the URL.
fileName Alanı
Açıklaması şöyle.
The file producer will by default "override" if a file already exists.
Açıklaması şöyle.
The fileName option supports the simple and file language
...
That means the file name can be dynamic computed...
Örnek - Dosya Taşır
Şöyle yaparız
context.addRoutes(new RouteBuilder() {
  @Override
  public void configure() {
    from("file://./?fileName=in.csv&noop=true")
      .to("file://./?fileName=out.csv")
      .end();
    }
});
Açıklaması şöyle.
...which will take the contents of the  in.csv  file from the current directory and copy all the contents for the  out.csv  file in the same directory. The  noop=true  parameter tells Camel not to do anything with the file. Without this option Camel would move the the file into  .camel  directory after processing it.
filter Alanı
İsim filtresi içindir
Örnek
Elimizde şöyle bir kod olsun
@Component
public class TestFileFilter implements GenericFileFilter {
  @Override
  public boolean accept(GenericFile file) {
    if (file.getFileName().matches("^[test_files]"))
      return true;
    return false;
  }
}
Şöyle yaparız
from("file:/testfolder/out/?filter=#testFileFilter")
    .routeId("testRoute")
    .to("file:/testfolder/in/")
    .end();
include Alanı
İsim filtresi içindir
Örnek
Şöyle yaparız
from("file:/testfolder/out/?include=test_files.*")
    .routeId("testRoute")
    .to("file:/testfolder/in/")
    .end();
noop Alanı
Açıklaması şöyle
'noop' configuration means that the files in the src/main/data directory should not be moved or deleted.

Örnek - Dosyayı ActiveMQ'ya taşır
Şöyle yaparız
context.addRoutes(new RouteBuilder() {
            
  @Override
  public void configure() throws Exception {
    from("file:input_box?noop=true")
    .to("activemq:queue:my_queue");
  }
});
readLock Alanı
Şöyle yaparız
file://${my.folder}?readLock=changed&readLockMinAge=${minAge}
  &inProgressRepository=#messageIdRepository&delay=${delay}&include=${include}

Camel RouteController Arayüzü

stopRoute metodu
routeId değeri test olan route nesnesini durdurmak için şöyle yaparızz
context.getRouteController().stopRoute("test", 1, TimeUnit.NANOSECONDS);

3 Ağustos 2020 Pazartesi

Camel Service Arayüzü

Örnek
Şöyle yaparız
@Service("myService")
public class MyService implements org.apache.camel.Service {

  @Override
  public void start() throws Exception {
    ...
  }

  @Override
  public void stop() throws Exception {
    ...
  }
    
}
Kullanmak için şöyle yaparız
@Autowired
private MyService myService;
@Autowired
private CamelContext context;

@PostConstruct
public void setupCamelContext() throws Exception {
  ....
  context.addService(myService);
}