Wednesday, May 26, 2010

Exposing a Java Class Via Spring and HTTP Invoker

Some time back I needed to expose a class as a "webservice", being lazy and crazy about Spring, I, like always, had a look at the framework that takes us all to a happy place.

a little while later, with a config here.. and a config there... A class exposed via http.

Ok so you need a web project with a web.xml that looks like:



    
  remoting
  
org.springframework.web.servlet.DispatcherServlet

  1
  
 
  remoting
  /httpremote/*
 


    
        
            30
        
    
    
        index.jsp
        
    


a remoting-servlet.xml Spring context file:



    
        
            
        
        
            bob.blog.NormalPojo
        
    





And the Java interface and class you want to expose:
package bob.blog;

public interface NormalPojo {

    void myExposedMethod(final String input);
    
}

package bob.blog;

public class NormalPojoImpl implements NormalPojo {

    public void myExposedMethod(String input) {
 System.out.println("Method Called with: " + input);
    }

}


That is all for the "Service" part... Once the war is deployed you can call it via http from a client. The client is also pretty simple, the reason I included the org.apache.commons.httpclient.HttpClient config in the application context was to be able to configure the timeout of the http call and one very nasty little thing you wouldn't expect. The default implementation is not thread safe.... that lead to some lets say "interesting" bugs, thankfully there is a thread safe implementation (MultiThreadedHttpConnectionManager, the one I used in the config below).

Hint: make sure the bean name in you server "/pojo-httpinvoker", the context in you web.xml "/httpremote/*" and client config all tie up "http://localhost:80/httpremote/pojo-httpinvoker".

You'll need an applicationContext.xml:



    
        
            
                
                    60000
                
            
        
        
            
                
                    
                        20
                    
                
            
        
    

    
        
    

    
        

http://localhost:80/httpremote/pojo-httpinvoker"

        
            bob.blog.NormalPojo
        
        
    




And then just use it as per any other Spring bean:

ApplicationContext ctx = new ClassPathXmlApplicationContext("bob/blog/applicationContext.xml");
NormalPojo testhttp = (NormalPojo) ctx.getBean("pojoHttp");
testhttp.myExposedMethod("whoo hoo");

2 comments:

  1. There is also...

    |bean id="httpServer" class="org.springframework.remoting.support.SimpleHttpServerFactoryBean"|
    |property name="contexts"|
    ...
    |/property|
    |property name="hostname" value="127.0.0.1" /|
    |property name="port" value="2345" /|
    |/bean|

    for simple stuff.

    ReplyDelete
  2. Thanks, I had not looked at that before actually, we had a fair amount of load and I had Weblogic servers available ... it is however a good way to go for stand alone apps. will definitely keep it in mind.

    ReplyDelete

Popular Posts

Followers