Updates from October, 2010 Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 11:01 am on October 13, 2010 Permalink |
    Tags: , archives, build, , , , script, ,   

    How-to: Get timestamp in ANT scripts 

    We use a lot of ANT scripts to build our archives. And archives without version number/timestamp is a big pain. We faced a lot of issues mainly integration issues. This is a small piece of script which can be used to add timestamp to an archive’s name.

    This is done using the tstamp task in ANT. tstamp is highly configurable and uses the java.text.SimpleDateFormat date/time patterns.

    <target name="timestamp.target">
    	<tstamp>
    		<format property="current.time"
    			pattern="yyyyMMdd_HHmmss" />
    		<format property="archive.name"
    			pattern="'MyArchive_'yyyyMMdd_HHmmss'.jar'" />
    	</tstamp>
    	<echo>${current.time}</echo>
    	<echo>${archive.name}</echo>
    </target>
    

    I got the following output when I tried to run the above build script:

    Buildfile: /home/subin/sampleProject/build.xml
    timestamp.target:
         [echo] 20101013_103329
         [echo] MyArchive_20101013_103329.jar
    BUILD SUCCESSFUL
    Total time: 524 milliseconds
    

    So, we have tstamp, which formats (format) the time/date information and assigns the result to the specified property. The echo statements demonstrate how we can use those values further.

    Hope this helps. Have a nice day.

     
  • Subinkrishna Gopi 4:51 pm on June 23, 2010 Permalink |
    Tags: cache, , distributed cache, ehcache,   

    Distributed Ehcache – RMI replication (configuration) 

    I am having some fun with Ehcache esp. in distributed caching. Distributed caching is really important in a clustered environment. Read more about distributed caching here – http://ehcache.org/documentation/distributed_caching.html.

    I have downloaded Ehcache 2.1 from http://sourceforge.net/projects/ehcache/files/ (ehcache-2.1.0-distribution.tar.gz, 48 mb approx.) which contains Ehcache core and the Terracotta libraries. Once if we have these libraries, we can configure the caches, peers and listeners. I have created a basic cache – MyCache – and my configuration file looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="ehcache.xsd"
             updateCheck="true" monitoring="autodetect"
             dynamicConfig="true" >
    
        <diskStore path="java.io.tmpdir"/>
        <cacheManagerEventListenerFactory class="" properties=""/>
    
        <defaultCache
               maxElementsInMemory="0"
               eternal="false"
               timeToIdleSeconds="1200"
               timeToLiveSeconds="1200">
        </defaultCache>
    
            <!-- For RMI replication (Setting the peer provider factory) -->
    	<cacheManagerPeerProviderFactory
    		class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    		properties="peerDiscovery=manual, rmiUrls=//remoteHostIpAddress:40000/MyCache"
    		propertySeparator="," />
    
    	<!-- For RMI replication (Setting the peer listener factory) -->
    	<cacheManagerPeerListenerFactory
                class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
                properties="hostName=localMachineIpAddress, port=40000, socketTimeoutMillis=120000"
                propertySeparator=","/>
    
    	<cache name="MyCache"
    		maxElementsInMemory="1000"
    		eternal="false"
    		overflowToDisk="true"
    		diskSpoolBufferSizeMB="20"
    		timeToLiveSeconds="3000"
    		timeToIdleSeconds="3000"
    		memoryStoreEvictionPolicy="LFU">
    
    		<!-- Cache event listener -->
    		<cacheEventListenerFactory
    			class="subin.cache.listener.factory.CacheEventListenerFactoryImpl"
    			properties="" />
    
    		<!-- RMI replication listener -->
    		<cacheEventListenerFactory
            	class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
            	properties="replicateAsynchronously=true,
    		         replicatePuts=true,
    		         replicatePutsViaCopy=true,
    		         replicateUpdates=true,
    		         replicateUpdatesViaCopy=true,
    		         replicateRemovals=true" />
    
    		<!-- RMI Cache bootstrap -->
    		<bootstrapCacheLoaderFactory
    			class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
    			properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"
    			propertySeparator="," />
    
    	</cache>
    
    </ehcache>
    

    Initializing the CacheManager & Accessing the cache

    // Testing the cache creation in ver 2.1
    CacheManager manager = new CacheManager("/path/to/config/file.xml");
    
    // Get the instance of "MyCache"
    Cache myCache = manager.getCache("MyCache");
    
    // Add an element to "MyCache". Its preferred to have both key and value
    // serializable
    Element aCacheElement = new Element(key, value);
    myCache.put(aCacheElement);
    

    Useful Links

    RMI replication: http://ehcache.org/documentation/distributed_caching_with_rmi.html
    Configuration: http://ehcache.org/documentation/configuration.html

     
  • Subinkrishna Gopi 3:26 pm on October 13, 2009 Permalink |
    Tags: , , , , , ,   

    How-to: Write web services using Axis2 (Part 2) 

    In my previous post I wrote about writing a web service provider using axis. But I did not mention a few things in that post.

    1. How to make the service accessible to the outside world
    2. How to write a Java client to avail the services

    1. How to make the service accessible to the outside world?

    As we are using the axis2.war as the way to deploy the services, we don’t really need to do anything. The deployment descriptor (web.xml) of axis2.war is equipped to handle it. But we need to make some URL mapping and stuffs like that.

    • Add appropriate URL-servlet mapping  in the web.xml
    • Add the name of the AAR in the service list

    a. Deployment descriptor changes:

     <servlet>
       <servlet-name>AxisServlet</servlet-name>
       <display-name>Apache-Axis Servlet</display-name>
       <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
     </servlet>
    
     <servlet-mapping>
       <servlet-name>AxisServlet</servlet-name>
       <url-pattern>/servlet/AxisServlet</url-pattern>
     </servlet-mapping>
    
     <servlet-mapping>
       <servlet-name>AxisServlet</servlet-name>
       <url-pattern>/services/*</url-pattern>
     </servlet-mapping>
    

    b. Add the name of the AAR in WEB-INF/services/services.list.

    2. Writing a Java client to access the services

    We have several ways to create the client stubs – JiXB, ADB etc. I followed ADB – Axis Data Binding. Axis provides a WSDL2Java tool to create client stubs from an existing WSDL. We can get the WSDL from http://localhost/services/SampleWs?wsdl.

    WSDL2Java -uri SampleWs.wsdl -p subin.rnd.ws.client -d adb -s -o clientStubSrcDirectory_name

    This will create the stub in the specified directory with name SampleWsStub.java. Once the stub is ready, we can write a client module which tries to access the deployed services.

    WsClient.java

    package subin.rnd.ws.client;
    
    import subin.rnd.ws.client.SampleWsStub.WsInput;
    import subin.rnd.ws.client.SampleWsStub.WsOutput;
    
    public class WsClient
    {
     public static void main(String[] args)
     throws Exception
     {
       SampleWsStub stub = new SampleWsStub("http://localhost/services/SampleWs");
    
       // Send the request
       SampleWsStub.DoSomething request = new SampleWsStub.DoSomething();
       WsInput anInput = new WsInput();
       anInput.setName("subin");
       request.setAnInput(anInput);
    
       // Get the response
       SampleWsStub.DoSomethingResponse response = stub.doSomething(request);
       WsOutput anOutput = response.get_return();
    
       System.out.println(anOutput.getResponseString());
     }
    }
    

    And that’s it !!!

     
  • Subinkrishna Gopi 3:45 pm on August 25, 2009 Permalink |
    Tags: , , , , , , ,   

    How-to: Write web services using Axis2 

    This is a very basic post on writing a web service provider and consumer using Axis2. You can find such posts anywhere in the web. Here I’ve tried to make it as simple as I can.

    Set up: What all we need to do?

    1. Axis2 runtime.
    I tried with WAR distribution from http://ws.apache.org/axis2/download/1_5/download.cgi
    2. A web/app server. I am using Tomcat.

    Using the Axis2 runtime & setting it up

    Extract the Axis2 WAR distribution and keep it in the deploy directory of the server. In case of Tomcat keep it in webapps. This is how my directory structure look like.

    image

    The WEB-INF is the most important directory. Tell you why. Unlike our normal WEB-INF in archives, this directory hold some special sub-directories. Here goes which contains what.

    classes – compiled Java classes. We can find some Axis specific classes here.
    conf – axis.xml (Axis configuration file)
    lib – All necessary Axis2 libraries (JARs)
    modules – Don’t ask me. Even I’m not sure. Did I mention that I’m also a beginner? 🙂
    services – All web service archives & services.list

    Coding: What we need to write?

    1. The service provider. A Java class.
    2. service.xml. The web services descriptor.
    3. build.xml. To build and deploy the web services archive.
    4. The service consumer. Another Java class to consume the services offered

    And we are good to go now. Let’s make our hands dirty with some Java code. This is very simple and kudos to Axis2.

    The service provider: SampleService.java

    package subin.rnd.ws;
    public class SampleService
    {
      public WsOutput doSomething(WsInput anInput)
      {
        System.out.println("doSomething()");
        WsOutput anOutput = new WsOutput();
        anOutput.setResponseString("I did some thing to " + anInput.getName());
        return (anOutput);
     }
    }
    

    WsInput is a sample input class to demonstrate that we can have more complex IO is possible. Similarly WsOutput is the output class. Instead of using WsInput / WsOutput for IO, we can use normal data types like integer, float, string etc too.

    WsInput.java

    package subin.rnd.ws;
    import java.io.Serializable;
    public class WsInput implements Serializable
    {
     private String name;
     public void setName(String name)
     {
       this.name = name;
     }
    
     public String getName()
     {
       return (this.name);
     }
    }
    

    WsOutput.java

    package subin.rnd.ws;
    import java.io.Serializable;
    public class WsOutput implements Serializable
    {
     private String responseString;
    
     public void setResponseString(String response)
     {
       this.responseString = response;
     }
     public String getResponseString()
     {
       return (this.responseString);
     }
    }
    

    We have the Java part of the web-service ready. But that’s not enough. We need to deploy the web-service as an AAR – Axis Archive – file. An AAR  is just another zip file (like a JAR) with a funky extension :D. The AAR should contain the class files along with the services.xml – web service descriptor.

    Web service descriptor: services.xml

    <service name="SampleWs" scope="application">
     <description>Subin's sample webs service</description>
     <messagereceivers>
     <messagereceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
     class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
     <messagereceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
     class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
     </messagereceivers>
     <operation name="doSomething" />
    <parameter name="ServiceClass">subin.rnd.ws.SampleService</parameter>
    </service>
    

    The services.xml is the place where we define the details of the services being offered.

    Build file: build.xml

    <project name="my.webservice.test" default="build.aar">
    <property name="deploy.dir" value=".../webapps/axis2.war/WEB-INF/services" />
    <property name="file.name" value="subinws.aar" />
    
     <target name="build.aar">
       <javac srcdir="src" destdir="bin" />
       <echo>Copying services.xml to bin</echo>
       <copy file="META-INF/services.xml"
           tofile="bin/META-INF/services.xml" overwrite="true"/>
       <jar basedir="bin" destfile="${file.name}" />
       <echo>Deleting services.xml from bin</echo>
       <delete dir="bin/META-INF" />
       <copy file="${file.name}" tofile="${deploy.dir}/${file.name}"  />
     </target>
    
    </project>
    

    So I hope we have an AAR ready, which is copied to the services directory. But we’ve not done yet. Now we have to make an entry in services/services.list file. Just insert the name of the ARR file at the end of it – in this case “subinws.aar”.

    Please wait for part 2.

     
  • Subinkrishna Gopi 11:55 am on June 18, 2009 Permalink |
    Tags: , exception, , , ,   

    A JBoss 5.1.0 Exception ! 

    jbosscorp_logo

    This is about another exception you may get while playing around with JBoss, Transactions, Remote EJBs etc. And the exception is:

    org.jboss.resource.JBossResourceException:
    Could not enlist in transaction on entering meta-aware object!

    My friend Venu had a post in his blog on why this exception happens and how to fix it in JBoss 5.0. You can read it here. But if we are using JBoss 5.1.0 this solution cannot help us. Because there are a couple of changes in the configuration in this new version of JBoss. In fact if you can take a look at the JBoss 5.1.0 GA release notes we can see a lot of changes in configuration especially if we are migrating from JBoss 5.0 to 5.1.

    And here is the solution.

    Open the JBOSS_HOME/server/JBOSS_CONFIGURATION/conf/jbossts-properties.xml. Set the following.

    <!-- Support subtransactions in the JTA layer? Default is NO. -->
    <property name="com.arjuna.ats.jta.supportSubtransactions" value="YES"/>
    

    Hope this helps.

    P.S. If you have any more details in this issue, please feel free to share it. Thank you.

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel