Create Axis2 module with Axis2M

This tutorial illustrates you how to create a Axis module with axis2m and package it as a war archive. We will create famous counter module to count our incoming and outgoing messages and log them in to a text file. You can find this sample in standard distribution under sample directory.

Step - 1 Create a project

Create a project using axis2 Quickstart archetype

mvn archetype:generate -DarchetypeCatalog=http://axis2m.sourceforge.net/repo

If you are not familiar with these please refer getting started guide

Step -2 Add necessary classes

Now we need to create required classes for our module, basically we need to develop three classes as follows.

  1. module class
  2. in-handler class
  3. out handler class

CounterModule class

public class CounterModule  implements Module, CounterConstants {
    private static final String COUNTS_COMMENT = "Counts";
    private static final String TIMESTAMP_FORMAT = "yyMMddHHmmss";
    private static final String FILE_SUFFIX = ".properties";
    
    
   

    public void init(ConfigurationContext configurationContext,
                     AxisModule axisModule) throws AxisFault {
        //initialize our counters
       // System.out.println("inside the init : module"+ getInfo());
        initCounter(configurationContext, INCOMING_MESSAGE_COUNT_KEY);
        initCounter(configurationContext, OUTGOING_MESSAGE_COUNT_KEY);
    }
}

IncomingCounterHandler

public class OutgoingCounterHandler extends AbstractHandler implements CounterConstants {
    public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {
        //get the counter property from the configuration context
        ConfigurationContext configurationContext = messageContext.getConfigurationContext();
        Integer count =
                (Integer) configurationContext.getProperty(OUTGOING_MESSAGE_COUNT_KEY);
        //increment the counter
        count = Integer.valueOf(count.intValue() + 1 + "");
        //set it back to the configuration
        configurationContext.setProperty(OUTGOING_MESSAGE_COUNT_KEY, count);
        //print it out
        System.out.println("The outgoing message count is now " + count);
        return InvocationResponse.CONTINUE;


    }

OutgoingCounterHandler

public class IncomingCounterHandler  implements CounterConstants ,Handler{
	
	
	 public String getInfoFromSpring(MessageContext messageContext){
		 
		 
		 ServletContext servletContext = (ServletContext) messageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT);  
		 WebApplicationContext wctx = 
				WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
	    	SpringService service=(SpringService)wctx.getBean("springService");
	    	return service.getMsg();
	    }

Step -3 create module descriptor

  1. Create a directory called ?CounterModule? inder src/main/web/WEB-inf /modules directory.
  2. Under a ?counteModuel? directory create a anther directory Called ?META-INF? , where you can create a your module description file called modules.xml.
  3. Copy the following content to your module.xml file

        Counts the incoming and outgoing messages
    bar

Step -4 deploy your module

You can deploy your module with embedded jetty or HTTP server using one of following commands

mvn jetty:run 
mvn axis2:run 

Once you run one of the above commands you can see in the console log that your module is loaded.

Step -5 test your module

So far we don?t know how this module actually work or not, so we need to engage it with a service in order to test it, we can use simpleServe class that available with axis2M quickstart , You don?t need to do any code level change to engage the module, just add the following line into service.xml .

Now use REST client or any other client to invoke your web service, in console you can see that our counter module in action.

Step 6 ? deploy your module

If you want to deploy our module and the service with web content you can use WAR packaging. Use default package command

mvn pacake 

Step 7 ? create MAR archive

MAR is axis archive format for modules, assume you want to distribute your counter module with your friends, so that they can use it with their services, the recommended way is to package modules as MAR, so how to do that ..?

Axis2 provide a Maven MAR plug-in, Axis2M quickstart project is preconfigured to use MAR plug-in, so just use following command

mvn mar:mar