28 Şubat 2021 Pazar

Camel TimerComponent Sınıfı

Örnek
Şöyle yaparız
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class RestAPIClientRoute extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    restConfiguration().host("dummy.restapiexample.com").port(80);
    
    from("timer:rest-client?period=10s")
      .to("rest:get:/api/v1/employees")
      .log("${body}");
  }
}

25 Şubat 2021 Perşembe

Camel Processors Kavramı

Giriş
Açıklaması şöyle
Processors handles things in between endpoints like routing, transformation, validation, enrichment, etc. 
Örnek
Şu satırı dahil ederiz
<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-activemq-starter</artifactId>
  <version>3.8.0</version>
</dependency>
<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-jackson-starter</artifactId>
  <version>3.8.0</version>
</dependency>
Elimizde ActiveMQ'dan okuyup processor'a gönderen şöyle bir kod olsun. Burada Processor olan GetEmployee .bean() metodu ile belirtiliyor. GetEmployee sınıfının tek metodu olduğu için hangi metodunun çağrılacağını belirtmeye gerek yok.
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class ActiveMQReceiverRoute extends RouteBuilder { @Autowired GetEmployee getEmployee; @Override public void configure() throws Exception { from("activemq:myqueue") .unmarshal().json(JsonLibrary.Jackson, Employee.class) .bean(getEmployee) .to("log:myloggingqueue"); } } @Component class GetEmployee{ Logger logger= LoggerFactory.getLogger(GetEmployee.class); public void getData(Employee employee){ logger.info("Emp data: "+employee.getId()); } }

17 Şubat 2021 Çarşamba

UnicodeUnescaper Sınıfı

Giriş
Şu satırı dahil ederiz
import org.apache.commons.text.translate.UnicodeUnescaper;
translate metodu
Şöyle yaparız
public class HTMLEncoder extends Common {

  public static String encode(String source) {
    String escaped = StringEscapeUtils.escapeJava(source);
    String utfChars = new UnicodeUnescaper().translate(escaped);
    return utfChars; 
  }
}

4 Şubat 2021 Perşembe

Lang3 EventListenerSupport Sınıfı

Giriş
Şu satırı dahil ederiz
import org.apache.commons.lang3.event.EventListenerSupport;
Sınıfın içi şöyle
public class EventListenerSupport<L> implements Serializable {

  // [...]

  /**
   * The list used to hold the registered listeners. This list is
   * intentionally a thread-safe copy-on-write-array so that traversals over
   * the list of listeners will be atomic.
   */
  private List<L> listeners = new CopyOnWriteArrayList<>();

  // [...]
}
create metodu
Örnek
Şöyle yaparız
import java.awt.event.ActionListener;
import org.apache.commons.lang3.event.EventListenerSupport;

EventListenerSupport<ActionListener> actionListeners = EventListenerSupport.create
(ActionListener.class);

public void someMethodThatFiresAction() {
  ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "somethingCool");
  actionListeners.fire().actionPerformed(e);
}