Suggested Pages

Tuesday, February 7, 2017

Maven Optional Dependency

Here an example of maven optional usage:

Maven Optional can be described as a dependency that is marked as excluded by default. This means that if your project X depends on a specific library Y that declares a dependency with Z as optional, then your project X does not include Z as library because it is considered optional.

Because of this all the functionalities of Y that does not regard Z can be used normally, but the functionality expressed by Z cannot be used by project X unless X declares a specific dependency on Z.

project Y
       
 
     <dependency>
      <groupId>groupZ</groupId>
      <artifactId>Z</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
     <optional>true</optional>
     </dependency>
As you can see project X should include and direct dependency on Z if the project X needs the functionality of the project Y expressed by project Z. project X
       
  <dependencies> 

     <dependency>
      <groupId>groupY</groupId>
      <artifactId>Y</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
    </dependency>
 
     <dependency>
      <groupId>groupZ</groupId>
      <artifactId>Z</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
       </dependency>

  </dependencies> 

Monday, February 6, 2017

Producer/Consumer Poison Pill

Poison pill is a technique used by a Producer to drug a Consumer in order to communicate the end of trasmission.

This kind of technique is generally used when a Producer and a Consumer share only one pipe for normal communication.

We can consider Poson Pill as a common practice when the pipe between Producer and Consumer is a datatype channel: the same channel is used to exchange different kind of data types between endpoints.

So because the datatype channel is used for different message types, it's usual to divide message into two categories:
- message data types;
- control message data types. 

A Poison Pill is a Control Message Data Type used to communicate the state of the entire channel: the intension of the Producer to stop itself.

Wednesday, February 1, 2017

Apache Camel Example : WebService Client

An example of Apache Camel that performs a set of commands every 5000 milliseconds in the following order:

  • Step1: the method getValue of the bean SampleBean is called (bean:samplebean?method=getValue);
  • Step2: the result of the Step1 is transformed with a Processor that append a string to the input (see Apache Camel Processor);
  • Step3: the result of the Step2 is used as input to call a Web Service whose endpoint is defined in the route-context.xml (see toUpperCaseEndpoint); The web service returns a value in uppercase of the input string;
  • Step4: the result of the Step3 is transformed by a new Processor that is responsible for adapting the format to the next endpoint ( "stream:file?fileName=C:\\Users\\mydoc\\example.txt );
  • Step5: the result of the Step4 is used to be written in a file named example.txt "stream:file?fileName=C:\\Users\\mydoc\\example.txt).

Example of Camel Context

The definition of the camel endpoint used in the route definition. route-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf"
 xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://camel.apache.org/schema/cxf 
     http://camel.apache.org/schema/cxf/camel-cxf.xsd
     http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">

 <camelContext xmlns="http://camel.apache.org/schema/spring">
     <package>it.example.route</package>
 </camelContext>

 <cxf:cxfEndpoint id="toUpperCaseEndpoint"
  address="http://localhost:8081/camel-jboss/ToUpperCaseBean" 
  wsdlURL="http://localhost:8081/camel-jboss/ToUpperCaseBean?wsdl" 
  serviceClass="it.example.ws.ToUpperCase"
  serviceName="t:ToUpperCaseBeanService"  
        endpointName="t:ToUpperCaseBeanPort"  
        xmlns:t="http://ws.apachecxf.simonefolinoblogspot.com/"/> 
        
       <bean id="samplebean" class="it.example.bean.SampleBean"/>

</beans>

Example of Java Bean

SampleBean.java
package it.example.bean;

public class SampleBean {

 public String getValue() {
  return "value of sample bean";
 }

 public void syso(){
  System.out.print("syso");
 }
}

SEI (Service Endpoint Interface)

ToUpperCase.java
package it.example.ws;

import javax.jws.WebService;

@WebService
public interface ToUpperCase {

 String toUpperCase(String s);
  
}

Web Service Implementation

ToUpperCaseBean.java
@Stateless(name="custom/ToUpperCaseBean", mappedName="custom/ToUpperCaseBean")
@WebService(endpointInterface="it.example.ws.ToUpperCase",targetNamespace="http://ws.apachecxf.simonefolinoblogspot.com/")
public class ToUpperCaseBean implements ToUpperCase {
 
 
 @WebMethod(operationName="toUpperCase")
 @WebResult(name = "return", targetNamespace = "http://ws.apachecxf.simonefolinoblogspot.com/")
 public String toUpperCase(String value) {
  System.out.println("value="+value);
  String upperCase = value.toUpperCase();
  System.out.println("upperCase="+upperCase);
  return upperCase;
 }

}  
}

Example of RouteBuilder

WebServiceRouteBuilder.java
package it.example.route;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

public class WebServiceRouteBuilder extends RouteBuilder {

 @Override
 public void configure() {

  from("timer://foo?fixedRate=true&period=5000").to("bean:samplebean?method=getValue").process(

   new Processor() {

   @Override
   public void process(Exchange exchange) throws Exception {
    if (exchange != null) {
     Message in = exchange.getIn();
     if (in != null) {
      in.setBody(in.getBody() +" i am the body");
      exchange.getOut().setBody(in);
     }
    }
   }
  }).to("cxf:bean:toUpperCaseEndpoint").setHeader("toUpperCase").body().process(

  new Processor() {

   @Override
   public void process(Exchange exchange) throws Exception {
    if (exchange != null) {
     Message in = exchange.getIn();
     if (in != null) {
      exchange.getOut().setBody(in.getBody());
     }
    }
   }
  }).to("stream:file?fileName=C:\\Users\\mydoc\\example.txt");

 }

}

Route Execution Output

This is the output of the execution of the camel route.
VALUE OF SAMPLE BEAN I AM THE BODY

Most searched words regarding Java

Suggested Pages