Wednesday, October 19, 2011

Spring MVC and Spring WS in the same app - how to's

Hosting Spring MVC controller and Spring-WS service in the same web application -

    This is fundamentally useful when developing an application which has its own view and additionally needs to host a bunch of services to its clients.  So how do we do this ?

1. web.xml of the web app needs to have 2 set of dispatcher servlets configured

    <servlet>
        <servlet-name>mvcControllerServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>soapServiceServlet</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>


Map these servlets to different URL's

   <servlet-mapping>
        <servlet-name>soapServiceServlet</servlet-name>
        <url-pattern>/appname/service/update</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>soapServiceServlet</servlet-name>
        <url-pattern>*.wsdl</url-pattern>
    </servlet-mapping>   
    <servlet-mapping>
        <servlet-name>mvcControllerServlet</servlet-name>
        <url-pattern>/app-name/mvc/*</url-pattern>
    </servlet-mapping>


2. Define 2 spring context XML's mvcControllerServlet-servlet.xml and soapServiceServlet-servlet.xml
   
     mvcControllerServlet-servlet.xml - Define all the mvc controllers in this one

    <!-- URL mapping definitions -->
    <bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
         <property name="mappings">
             <props>
                 <prop key="**/info">InfoController</prop>
              
            </props>
        </property>
    </bean>

   
    <bean id="InfoController" class="com.test.web.InfoController" >
        <property name="successView">
            <value>mvc/success</value>
        </property>
       </bean>




    soapServiceServlet-servlet.xml - Define messageReceiver etc in this one to facilitate spring SOAP message handling

    <bean id="messageReceiver" class="org.springframework.ws.soap.server.SoapMessageDispatcher"/>  

    <bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
        <property name="endpointMap">
            <map>
            <entry key="{http://www.testapp.com/service}UpdateRequest" value-ref="endpoint" />
            </map>
        </property>
    </bean>


Service implementation goes into the endpoint class and mvc implementation can be written in InfoController.

Simple and neat.



 

Monday, October 17, 2011

CHART - Java collection framework perfromance





Also Read
The Clean Coder

Wednesday, October 05, 2011

How to get C like performance in Java


  • The JVM does implicit bounds checking on array access and updates. This has a small overhead - you can unsafely eliminate this (and open yourself to buffer overflows and other problems) using the the Unsafe class or direct buffers.
  • Use memory-minimized collections to reduce memory usage.
  • You can use Direct memory to store data how you wish (this is what BigMemory uses).
  • Use blocking IO in NIO (which is the default for a Channel) - don't use Selectors unless you need them.
  • Most systems can handle 1K-10K threads efficiently. Scalability beyond 10K users/server doesn't buy you anything in the real world since the server resources will be consumed servicing 10k concurrent users.
  • -XX:+UseCompressedStrings use byte[] instead of char[] for strings which don't need 16-bit characters - this saves memory but is 5%-10% slower.
  • To reduce string space usage, you can use your own Text type which wraps a byte[], or get your text data from ByteBuffer, CharBuffer or use Unsafe or -XX:+UseCompressedStrings.
  • To start the JVM faster, load fewer libraries.
  • Use primitives instead of primitive wrapper objects.