Skip to main content

JBoss Fuse - Implementing WebService with Camel using CXF SOAP and Restful with CXFRS. Part One

Some friend of mine in Japan asking if I could do a simple demo on Camel CXF,  So here it is :).
This demo is about an insurance simple online claim application and status update. I will show you how to use Camel CXF to quickly setup an WebService, also setup Restful WebServices in few very easy steps.

In this demo we are going to setup a web service that takes in customer's insurance claim application, also provide another cancel function in case they have changed their mind.

One of the reason I love so much about Camel is that I don't need to deal with different context of the data, like XML, JSON... I mean they are ok, but I like POJO more, as a Java developer they are more meaningful and much easier to play with. So now we are going define some POJOs that we are going to exchange in this demo.

These two POJO contains the application information required (ClaimInput) and the result of the application submission (ClaimOutput).
package org.blogdemo.claimdemo;

import java.util.Date;

public class ClaimInput {
	
	String customerName = "";
	String customerId="";
	Date claimDate= null;
	String contactPhone = "";
	String email = "";
	String polno = "";
	int applyItem = 0;
	int claimType = 0;
	
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getCustomerId() {
		return customerId;
	}
	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}
	public Date getClaimDate() {
		return claimDate;
	}
	public void setClaimDate(Date claimDate) {
		this.claimDate = claimDate;
	}
	public String getContactPhone() {
		return contactPhone;
	}
	public void setContactPhone(String contactPhone) {
		this.contactPhone = contactPhone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPolno() {
		return polno;
	}
	public void setPolno(String polno) {
		this.polno = polno;
	}
	public int getApplyItem() {
		return applyItem;
	}
	public void setApplyItem(int applyItem) {
		this.applyItem = applyItem;
	}
	public int getClaimType() {
		return claimType;
	}
	public void setClaimType(int claimType) {
		this.claimType = claimType;
	}
	
	

}


package org.blogdemo.claimdemo;

public class ClaimOutput {

	String customerName = "";
	String claimNo = "";
	String status = "";
	
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getClaimNo() {
		return claimNo;
	}
	public void setClaimNo(String claimNo) {
		this.claimNo = claimNo;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	
	
	
}


And a third POJO ClaimStatus is for showing the status of application cancel submission.
package org.blogdemo.claimdemo;

public class ClaimStatus {
	String status = "";
	String customerID = "";
	String polno ="";
	String claimNo="";
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getCustomerID() {
		return customerID;
	}
	public void setCustomerID(String customerID) {
		this.customerID = customerID;
	}
	public String getPolno() {
		return polno;
	}
	public void setPolno(String polno) {
		this.polno = polno;
	}
	public String getClaimNo() {
		return claimNo;
	}
	public void setClaimNo(String claimNo) {
		this.claimNo = claimNo;
	}
	
	
}


Now we have all the hard part done, (Trust me, it's always defining the model that is the hardest.) We can now start on creating the web service in our camel route.

To define a web service there are 2 main steps.
1. Define an interface of the web service
2. Setup the endpoint in Camel context,

Once you get those two ready, you are pretty much done, so we will create a "ClaimService" interface, that specify two operations in our insurance claim application, apply and cancel. Have a look at the code, notice the apply method takes in ClaimInput as parameter and ClaimOutput as the return value. And for cancel method, we take in the claim number (String) and return ClaimStatus as the output.


package org.blogdemo.claimdemo;

public interface ClaimService {
	
	public ClaimOutput apply(ClaimInput input);
	public ClaimStatus cancel(String claimNo);

}

Then we go back to the camel context xml, and register our web service interface using camel-cxf component. So all you have to do it to specify the address of the web service and point the endpoint to the service class, which is the interface we just created. 

<cxf:cxfEndpoint id="claimEndpoint" address="http://localhost:9191/cxf/claim" serviceClass="org.blogdemo.insurancedemo.ClaimService" />

Lastly, the business processing part, here I am going to just place some constant value inside, but in normal world, this is where you place the actual processing logic. 

package org.blogdemo.claimdemo;

public class ClaimProcessor {
	public ClaimOutput process(ClaimInput input) throws Exception {
        // get the id of the input
    	//ClaimInput input = exchange.getIn().getBody(ClaimInput.class);

    	System.out.println(input);
    	
    	// set reply including the id
        ClaimOutput output = new ClaimOutput();
        output.setClaimNo("A00099484");
        output.setCustomerName(input.getCustomerName());
        output.setStatus("DONE");
        //exchange.getOut().setBody(output);
        return output;
    }
	
	 public ClaimStatus cancel(String claimNo) throws Exception {
	      
			ClaimStatus status = new ClaimStatus();
		   	status.setClaimNo(claimNo);
		   	status.setStatus("OK");
		   	
		   	return status;
	   }
}


Then we are going to put it together, here is what the camel route looked like. Notice we uses the operation name header to determine the process we want to pass on to.





  
  
  
  
    
        
        
        
            direct:${header.operationName}
        
    
    
        
        
    
    
        
        
    
 



And don't forget to update the pom.xml for all dependencies needed for this demo.
  
    
  		org.ow2.asm
		asm-all
		4.1
	
    
      org.apache.camel
      camel-core
      2.12.0.redhat-610379
    
    
      org.apache.camel
      camel-blueprint
      2.12.0.redhat-610379
    
    
	  org.apache.camel
	  camel-jaxb
	  2.12.0.redhat-610379
    
	
	  org.apache.camel
	  camel-jackson
	  2.12.0.redhat-610379
	    
	
    
      org.apache.camel
      camel-http4
      2.12.0.redhat-610379
	  
	
	
	 
	    org.apache.camel
	    camel-cxf
	    2.12.0.redhat-610379
	
		
	
	    org.apache.cxf
	    cxf-rt-frontend-jaxrs
	    2.7.0.redhat-610379
	
	
	
	    org.apache.cxf
	    cxf-rt-transports-http-jetty
	    2.7.0.redhat-610379
	
	
		


    
    
      org.slf4j
      slf4j-api
      1.7.5
    
    
      org.slf4j
      slf4j-log4j12
      1.7.5
    
    
      org.slf4j
      jcl-over-slf4j
      1.7.5
    
    
      log4j
      log4j
      1.2.17
    

    
    
      org.apache.camel
      camel-test-blueprint
      2.12.0.redhat-610379
      test
    
  

Now start the route by running camel:run in maven. The Registry URL will be http://localhost:9191/cxf/claim for now.


We can start the application, by using the SOAP UI tool, create a new project with the WSDL address as http://localhost:9191/cxf/claim?wsdl, then we can now play with the web service we created!

Here is the video:



There is another form of web service,  Restful web service, next part of the demo, I will show you how to build it and how to create CXF producer in Camel.

Thanks! 

Comments

Thanks for tutorial Christina, I am getting y: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: claimEndPoint of type: org.apache.camel.component.cxf.CxfEndpoint
exception , can you please help me on this
Christina Lin said…
Did you add the camel-cxf related dependencies in the pom.xml?
I was missing the POM dependency of camel-cxf ,thank you for help :)
kanav aggarwal said…
Hi Christina,

Please share code link for this project.
Thank you in advance.

Thanks,
Kanav
Unknown said…
Hi Christina,

Please share the code, am getting the error below when running the project.

[mel.test.blueprint.Main.main()] CamelBlueprintHelper INFO Deleting work directory target/bundles/1436534148555
[ERROR] *************************************
[ERROR] Error occurred while running main from: org.apache.camel.test.blueprint.Main
[ERROR]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.camel.maven.RunMojo$1.run(RunMojo.java:488)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: Gave up waiting for service (objectClass=org.apache.camel.CamelContext)
at org.apache.camel.test.blueprint.CamelBlueprintHelper.getOsgiService(CamelBlueprintHelper.java:240)
at org.apache.camel.test.blueprint.CamelBlueprintHelper.getOsgiService(CamelBlueprintHelper.java:202)
at org.apache.camel.test.blueprint.Main.doStart(Main.java:104)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.main.MainSupport.run(MainSupport.java:148)
at org.apache.camel.main.MainSupport.run(MainSupport.java:352)
at org.apache.camel.test.blueprint.Main.main(Main.java:81)
... 6 more
[ERROR] *************************************
kanav aggarwal said…
Hi friend,

Generally this issue comes when u have a jar file missing.
I mean dependency missing from your pom file or from your server.

Thanks
Kanav
Samuel Lima said…
Christina,

Do you have any example of JBoss Fuse cosuming SOAP WebService with Camel using CXF?
I've seen in the homeloan demo that consuming REST is easer than SOAP.

Thanks,
Samuel Lima
Samuel Lima said…
Sorry,

To be more clear, what I meant is a route that calls a SOAP Web Service, passes parameters, get the results and do some work with the results which are in SOAP format.

Thanks.
Christina Lin said…
Samuel, OK, sure, will put that on my list! :)

Pravin Deshmukh said…
This comment has been removed by the author.
hi Christina

I'm getting the following error:

ERROR Unable to start blueprint bundle container for claimdemo.
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'cxf: cxfendpoint'.

Can you help me?

Thanks in advance :)

Greetings!
Hello Christina,

I am getting below starrup error after successful deployment

09:45:13,578 | WARN | qtp1808615023-63 | ServletController | 123 - org.apache.cxf.cxf-rt-transports-http - 3.0.4.redhat-620133 | Can't find the the request for http://localhost:8181/cxf//'s Observer
09:47:28,089 | INFO | rint Extender: 1 | BlueprintCamelContext | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | Apache Camel 2.15.1.redhat-620133 (CamelContext: blueprintContext) is shutting down
09:47:28,119 | INFO | rint Extender: 1 | BlueprintCamelContext | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | Apache Camel 2.15.1.redhat-620133 (CamelContext: blueprintContext) uptime not started
09:47:28,119 | INFO | rint Extender: 1 | BlueprintCamelContext | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | Apache Camel 2.15.1.redhat-620133 (CamelContext: blueprintContext) is shutdown in 0.080 seconds
| ERROR | rint Extender: 1 | BlueprintContainerImpl | 21 - org.apache.aries.blueprint.core - 1.4.2 | Unable to start blueprint container for bundle claimdemo due to unresolved dependencies [(&(dataformat=json-jackson)(objectClass=org.apache.camel.spi.DataFormatResolver))]
java.util.concurrent.TimeoutExceptio

In JBoss Developer able to start successfully

Thanks in Advance!
Krishna
Unknown said…
This comment has been removed by the author.
Chanti Chanti said…
can you write post or example on csfrs REST webservice consuming remote rest url.

Regards,
Chanti

Popular posts from this blog

Red Hat Fuse - Announcing Fuse 7 Tech preview 3 release.

Red Hat Fuse 7.0 technical preview three is out today! On the pathway to become one of the best cloud-native integration platform, Fuse gives developer freedom to choose how they want to develop the integration solution, where they want to deploy it and capabilities to address new integration personas that do not have development experience.
By supporting the three major runtime, developer is free to work on the runtime of their choice.By supporting standalone and cloud deployment, it simplifies the complexity to distinguish between these environments, allowing application to deploy freely among the environment of your choice. All levels of developers are welcome, you can either dive deep into creating customize complex integration logic, or using the new low code platform to quickly build a simple integration. In this Tech Preview release you get it all.
Fuse StandaloneSpring-boot for microserviceKaraf 4 for OSGi loverJBoss EAP for JavaEE developersFuse on OpenShiftPlugins for easy co…

JBoss EAP 6 - 效能調校 (一) DataSource 的 Connection Pool

效能沒有什麼Best Practice, 反正能調整的就那些。 通常,一個程式的效能大概有70-80% 都跟程式怎麼寫的其實比較有關係。

最近我最疼愛的小貓Puji 因為膀胱結石開刀的時候過世了,心情很差請原諒我的口氣沒有很好,也沒有心情寫部落格。

Puji R.I.P.

=======================正文=======================

這個題目很多人叫我寫,可是這題目好大,這分明就是整死我咩~
所以我會分幾段慢慢寫。

JBoss 的 SubsystemDatasource WebWeb Service EJB Hibernate JMSJCAJVM 調校OS (作業系統)

先來看一下 DataSource Subsystem, DataSource 的部分主要是針對Connection Pool 做調校。

通常,程式都會需要跟資料庫界接,電腦在本機,尤其是在記憶體的運算很快,但是一旦要外部的資源連接,就是會非常的耗資源。所以現在的應用程式伺服器都會有個Pool 放一些先連接好的 資料庫connection,當程式有需要的時候就可以馬上提供,而不用花那些多餘的資源去連接資料庫。

這就是為什麼要針對Connection Pool 去做調校。

以下會討論到的參數,都是跟效能比較有關係,Datasource 還有很多參數,像是檢核connection 是否正確的,我都不會提到。如果你追求的是非常快速的效能,那我建議你一個檢核都不要加。當然,這樣就會為伺服器上面執行的程式帶來風險。這就是你要在效能與正確,安全性上面的取捨了。 (套句我朋友說的話,不可能又要馬兒好,又要馬兒不吃草的..)

最重要的調校參數就是 Connection 的 Pool 數量。(也就是那個Pool 裡面要放幾條的connection.) 這個參數是每一個應用程式都不一樣的。

min-pool-size 

Connection Pool 最少會存留的connection 數量

max-pool-size 

Connection Pool 最多可以開啓的 connection 數量

prefill

事先將connection pool 裡面建立好min-pool-size 的connection.

我的建議是觀察一下平常程式要用到的量設定為 min-pool-size 。
加上…

JBoss Fuse - Fuse workshop 101 - Part One

On my way to Hong Kong for a day of workshop on JBoss Fuse, and as I go through my Slide deck, I cannot find any decent easy workshop for beginners. Therefore I decide make a workshop that is easy for Camel first timer to get their hands dirty.

The first of part of the workshop is an introduction to Camel, it first goes through what is exactly inside JBoss Fuse.

For part one of the workshop, it takes your through the very basic of Camel, one of the very important component inside JBoss Fuse.
Every Camel need to have a runtime container to run in, inside camel we call it a CAMEL CONTEXT.  Inside every Camel context, you can define lots of camel route and registry, don't worry about what those are, we will explain later.


So inside out blueprint xml, you will see a tag called camelContext.



Next up is camel route, they are a chain of command or process defined by you, as a developer.
Inside the camel route, there are consumer endpoints that listens to the incoming messages, producers …