Thursday, July 10, 2014

Use Spring Integration To Consume Soap Web Services

Spring Integration Consume Soap Web Service




 //interface  
 import javax.jws.WebMethod;  
 import javax.jws.WebParam;  
 import javax.jws.WebService;  
 import javax.jws.soap.SOAPBinding;  
 /**  
  *  
  * @author  
  */  
 @WebService  
 @SOAPBinding(style= SOAPBinding.Style.DOCUMENT,parameterStyle= SOAPBinding.ParameterStyle.BARE,use= SOAPBinding.Use.LITERAL)  
 public interface IService {  
   @WebMethod(operationName="calculateSomething")  
   public String calculateSomething();  
 }  
 //implementation  
 import java.util.Date;  
 import javax.jws.WebService;  
 import javax.xml.ws.*;  
 import javax.jws.*;  
 /**  
  *  
  * @author  
  */  
 @WebService  
 public class ServiceClass implements IService {  
   public static void main(String ... args){  
     ServiceClass sc = new ServiceClass();  
     Endpoint.publish("http://127.0.0.1:15041/service", sc);  
     System.out.println("Server Started: " + new Date().toString());  
   }  
   public String calculateSomething() {  
    System.out.println(" method called " + new Date());  
  return new Date().toString();  
   }  
 }  
 //consumer  
 import org.springframework.context.support.ClassPathXmlApplicationContext;  
 import org.springframework.integration.support.MessageBuilder;  
 import org.springframework.integration.support.channel.BeanFactoryChannelResolver;  
 import org.springframework.messaging.Message;  
 import org.springframework.messaging.MessageChannel;  
 import org.springframework.messaging.core.DestinationResolver;  
 public class MyConsumer {  
  public static void main(String[] args) {  
  ClassPathXmlApplicationContext context =  
   new ClassPathXmlApplicationContext("/META-INF/spring/integration/ws-context.xml");  
  DestinationResolver<MessageChannel> channelResolver = new BeanFactoryChannelResolver(context);  
  // Compose the XML message according to the server's schema  
  String requestXml =   
    "<calculateSomething xmlns=\"http://samplenamespacefromwsdl/\">"+  
  "</calculateSomething>";  
  // Create the Message object  
  Message<String> message = MessageBuilder.withPayload(requestXml).build();  
  // Send the Message to the handler's input channel  
  MessageChannel channel = channelResolver.resolveDestination("myChannel");  
  boolean response = channel.send(message);  
  System.out.println("channel: "+channel.toString());  
  System.out.println("msg sent: "+response + " payload " + message.getPayload());  
  }  
 }  
 //spring integration context  
 <?xml version="1.0" encoding="UTF-8"?>  
 <beans:beans xmlns="http://www.springframework.org/schema/integration"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xmlns:beans="http://www.springframework.org/schema/beans"  
  xmlns:stream="http://www.springframework.org/schema/integration/stream"  
  xmlns:ws="http://www.springframework.org/schema/integration/ws"  
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
   http://www.springframework.org/schema/beans/spring-beans.xsd  
   http://www.springframework.org/schema/integration  
   http://www.springframework.org/schema/integration/spring-integration.xsd  
   http://www.springframework.org/schema/integration/stream  
   http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd  
   http://www.springframework.org/schema/integration/ws  
   http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd">  
  <chain input-channel="myChannel" output-channel="outChannel">  
  <!--ws:header-enricher>  
   <ws:soap-action value=""/>  
  </ws:header-enricher-->  
  <ws:outbound-gateway uri="http://localhost:15041/service"/>  
  </chain>  
  <!-- The response from the service is logged to the console. -->  
  <stream:stdout-channel-adapter id="outChannel"/>   
 </beans:beans>  
 // For The Maven Dependencies  
 <dependencies>  
 <dependency>  
 <groupId>org.springframework.integration</groupId>  
 <artifactId>spring-integration-stream</artifactId>  
 <version>4.0.0.RELEASE</version>  
 <scope>compile</scope>  
 </dependency>  
 <dependency>  
 <groupId>junit</groupId>  
 <artifactId>junit</artifactId>  
 <version>4.11</version>  
 <scope>test</scope>  
 </dependency>  
 <dependency>  
 <groupId>org.hamcrest</groupId>  
 <artifactId>hamcrest-all</artifactId>  
 <version>1.3</version>  
 <scope>test</scope>  
 </dependency>  
 <dependency>  
 <groupId>org.springframework.integration</groupId>  
 <artifactId>spring-integration-ws</artifactId>  
 <version>4.0.0.RELEASE</version>  
 <scope>compile</scope>  
 </dependency>  
 <dependency>  
 <groupId>log4j</groupId>  
 <artifactId>log4j</artifactId>  
 <version>1.2.17</version>  
 <scope>compile</scope>  
 </dependency>  
 <dependency>  
 <groupId>org.mockito</groupId>  
 <artifactId>mockito-core</artifactId>  
 <version>1.9.5</version>  
 <scope>test</scope>  
 </dependency>  
 <dependency>  
 <groupId>org.springframework</groupId>  
 <artifactId>spring-test</artifactId>  
 <version>4.0.3.RELEASE</version>  
 <scope>test</scope>  
 </dependency>  
 </dependencies>