Notice

╠ This is my personal blog and my posts here have nothing to do with my employers or any other association I may have. It is my personal blog for my personal experience, ideas and notes. ╣

Monday, September 1, 2014

Using Spring HTTP Invoker


Spring provide features to integrate classes for remoting support using various technologies like Remote Method Invocation (RMI), Hessian, Burlap, JAX-WS,Java Messaging Service (JMS), Advanced Messaging Queuing Protocol (AMQP) and Spring HTTP Invoker.It is recommended that you must have bit of idea about proxy pattern as Spring remoting framework follow the Proxy software pattern. Here we will be looking into Spring HTTP Invoker only. 

Spring HTTP Invoker comes into picture when Spring recognizes the need of HTTP based client server communication and which uses java serialization because RMI  services uses java serialization but it difficult to work with across firewall and on the other side HTTP based services such as Hessian and Burlap work well across firewall but they use there proprietor serialization method not java serialization.  


Spring HTTP Invoker is a special remoting strategy which allows for Java serialization via HTTP, supporting any Java interface clients to communicate with Spring server using HTTP service without client knowledge of implementation. As it is working on HTTP service it will be easy to use it across network with firewall.
 
Spring HTTP Invoker Flow
Fig.1. Spring HTTP Invoker Flow

Spring HTTP Invoker flow steps:-
  • Client invoke the proxy's service method which is being handle by HttpInvokerProxyFactoryBean.
  • The proxy convert the method call to HTTP remote call.
  • The HTTP Service Adapter intercept to remote call and create HttpInvokerServiceExporter.
  • It the forward the method call to Service. 




Dispatcher Flow
Fig. 2. Dispatcher Flow

Spring HTTP invoker remote service is simple and easy. Lets get into the implementation of Spring HTTP Invoker client server model using web container and without web container.

First we start where Spring HTTP Invoker server running in web container.

Step 1. Create interface for the client


Application.java
  1. package andy.blog.spring.remoting.httpinvoker.service;
  2. public interface Application {
  3. String getGreeting(final String name);
  4. }
Step 2. Create the service for client

ApplicationService.java
  1. package andy.blog.spring.remoting.httpinvoker.service;
  2. public class ApplicationService implements Application {
  3. @Override
  4. public String getGreeting(String name) {
  5. return "Hi " + name + " !!";
  6. }
  7. }
Step 3. Create the web.xml
web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id="WebApp_ID" version="3.0">
  6. <display-name>Application</display-name>
  7. <welcome-file-list>
  8. <welcome-file>pages/index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <servlet>
  11. <servlet-name>Application</servlet-name>
  12. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  13. <init-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>/WEB-INF/WebAppContext/SpringServletContext.xml
  16. </init-param>
  17. <load-on-startup>1</load-on-startup>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>Application</servlet-name>
  21. <url-pattern>*.http</url-pattern>
  22. </servlet-mapping>
  23. </web-app>
Step 4. Create the Spring Servlet Application context. 

This is where we exposing the beans as HTTP service. In case of web application request handler by DispatcherServlet then dispatches to HTTP invoker service. This service translate them into method call in Spring manages beans.  
Here applicationService is the service bean and /greeting.http is Spring HTTP service exporter bean.
      
WEB-INF\WebAppContext\SpringServletContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/mvc"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:beans="http://www.springframework.org/schema/beans"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9. <annotation-driven />
  10. <context:component-scan base-package="andy.blog.spring.remoting.httpinvoker.controller" />
  11. <beans:bean
  12. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  13. <beans:property name="prefix" value="/pages/" />
  14. <beans:property name="suffix" value=".jsp" />
  15. </beans:bean>
  16. <!-- Application Service -->
  17. <beans:bean id="applicationService" class="andy.blog.spring.remoting.httpinvoker.service.ApplicationService" />
  18. <!-- Exporter -->
  19. <beans:bean name="/greeting.http"
  20. class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
  21. <!-- Application Service reference -->
  22. <beans:property name="service" ref="applicationService" />
  23. <!-- Application interface -->
  24. <beans:property name="serviceInterface" value="andy.blog.spring.remoting.httpinvoker.service.Application" />
  25. </beans:bean>
  26. </beans:beans>
Step 5. Application Context of Spring HTTP Invoker client. Here HttpInvokerProxyFactoryBean is a proxy factory for creating HTTP Invoker service proxies. serviceUrl is the service url where client send the HTTP request and it is create HTTP invoker service exporter object and invokes the actual service method call.
applicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="applicationClient"
  7. class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
  8. <property name="serviceUrl"
  9. value="http://localhost:9090/SpringHttpService/greeting.http" />
  10. <property name="serviceInterface" value="andy.blog.spring.remoting.httpinvoker.service.Application" />
  11. </bean>
  12. </beans>
Step 6. To access service via HTTP create the Client.
ApplicationClient.java
  1. package andy.blog.spring.remoting.httpinvoker.client;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
  5. import andy.blog.spring.remoting.httpinvoker.service.Application;
  6. public class ApplicationClient {
  7. public static void main(String...pram) {
  8. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  9. HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = context.getBean(HttpInvokerProxyFactoryBean.class);
  10. Application application = (Application) httpInvokerProxyFactoryBean.getObject();
  11. System.out.println("Start Application Client ");
  12. System.out.println(application.getGreeting("Tom"));
  13. System.out.println(application.getGreeting("Harry"));
  14. System.out.println(application.getGreeting("James"));
  15. System.out.println(application.getGreeting("Andy"));
  16. System.out.println("End Application Client ");
  17. }
  18. }
Run the Spring HTTP Invoker server side into some web container and the run the client.

Start Application Client
Hi Tom !!
Hi Harry !!
Hi James !!
Hi Andy !!
End Application Client

 
Now we will see how to create Spring HTTP Invoker server running in non-web container and how to write a test case for that.
Repeat Step 1 create an interface for the client.

We will re-write the HTTP service method to distinguishes from above service method. Just changed the return message text.


ApplicationService.java
  1. package andy.blog.spring.remoting.httpinvoker.service;
  2. public class ApplicationService implements Application {
  3. @Override
  4. public String getGreeting(String name) {
  5. return "Hi " + name + " !! This is non web service"; // message is changed
  6. }
  7. }
Simple HTTP server applicationContext.xml here application service & HTTP service exporter are same as above discussed but we here used org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter instead of HttpInvokerServiceExporter. Here
org.springframework.remoting.support.SimpleHttpServerFactoryBean is used to create simple HTTP Server based on the HTTP server that is included in Sun's JRE.
applicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  5. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:util="http://www.springframework.org/schema/util">
  8. <context:annotation-config />
  9. <!-- Application Service -->
  10. <bean name="applicationService"
  11. class="andy.blog.spring.remoting.httpinvoker.service.ApplicationService" />
  12. <!-- Exporter -->
  13. <bean name="serviceExporter"
  14. class="org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter">
  15. <property name="serviceInterface"
  16. value="andy.blog.spring.remoting.httpinvoker.service.Application" />
  17. <property name="service" ref="applicationService" />
  18. </bean>
  19. <!-- Http Web Server -->
  20. <bean id="httpServer"
  21. class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
  22. <property name="contexts">
  23. <util:map>
  24. <entry key="/SpringHttpService/greeting.http" value-ref="serviceExporter" />
  25. </util:map>
  26. </property>
  27. <property name="port" value="9090" />
  28. </bean>
  29. <!-- For JUnit Test -->
  30. <bean id="remotedApplicationService"
  31. class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
  32. <qualifier value="remoted" />
  33. <property name="serviceUrl"
  34. value="http://localhost:9090/SpringHttpService/greeting.http" />
  35. <property name="serviceInterface"
  36. value="andy.blog.spring.remoting.httpinvoker.service.Application" />
  37. </bean>
  38. </beans>
We have to create a simple HTTP server using Sun's package. This is required to to run the HTTP server for our client application.
ApplicationServer.java
  1. package andy.blog.spring.remoting.httpinvoker.service;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.sun.net.httpserver.HttpServer;
  5. public class ApplicationServer {
  6. public static void main(String[] args) {
  7. ApplicationContext context = new ClassPathXmlApplicationContext("/applicatonContext.xml");
  8. HttpServer appServer = (HttpServer) context.getBean("httpServer");
  9. }
  10. }
Test case to make sure our HTTP invoker service work's fine.
ApplicationServiceTest.java
  1. package andy.blog.spring.remoting.httpinvoker.service;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertTrue;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Qualifier;
  8. import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
  9. import org.springframework.test.context.ContextConfiguration;
  10. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
  11. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  12. @RunWith(SpringJUnit4ClassRunner.class)
  13. @ContextConfiguration(inheritLocations = true, locations = { "/applicatonContext.xml" })
  14. public class ApplicationServiceTest extends AbstractJUnit4SpringContextTests {
  15. @Autowired(required = true)
  16. @Qualifier("remoted")
  17. private Application application;
  18. @Test
  19. public void getGreetingTestUsingConfiguredClient() throws Exception {
  20. String greeting = application.getGreeting("Tom");
  21. assertEquals("Hi Tom !! This is non web service", greeting);
  22. System.out.println(greeting);
  23. assertTrue("Should not be the implementation", !application.getClass().equals(ApplicationService.class));
  24. }
  25. @Test
  26. public void getGreetingTestManually() throws Exception {
  27. HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
  28. proxy.setServiceInterface(Application.class);
  29. proxy.setServiceUrl("http://localhost:9090/SpringHttpService/greeting.http");
  30. proxy.afterPropertiesSet();
  31. Application application = (Application) proxy.getObject();
  32. String greeting = application.getGreeting("tester");
  33. assertEquals("Hi tester !! This is non web service", greeting);
  34. System.out.println(greeting);
  35. assertTrue("Should not be the implementation", !application.getClass().equals(ApplicationService.class));
  36. }
  37. }
If you run the above test or above client you get below output.
Hi tester !! This is non web service
Hi Tom !! This is non web service


Well Spring HTTP Invoker is quite easy to implement you just need a reason to run two application on different JVMs.  

No comments:

Post a Comment