Wednesday, April 27, 2011

Sample java code for start axis2 simple http server with service

Some times you may want to use sample web service to communicate with your client written by java.This example will show you how to start/stop apache simple HTTP server programatically and upload axis2 service to the simple axis2 service programatically.

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.deployment.DeploymentEngine;
import org.apache.axis2.description.AxisServiceGroup;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.axis2.transport.http.SimpleHTTPServer;

import java.io.File;

public class HTTPServerManager {
private static SimpleHTTPServer server;
protected int serverPort = 9000;

public synchronized void startHTTPServer() {
ConfigurationContext context;
try {
context = ConfigurationContextFactory.
createConfigurationContextFromFileSystem(location of axis2.xml, null);
server = new SimpleHTTPServer(context, serverPort);
String resourcePath = HTTPServerManager.class.getResource("location of axis2service.aar").getPath();
AxisServiceGroup serviceGroup = DeploymentEngine.loadServiceGroup(new File(resourcePath), context);
AxisConfiguration xConfig = context.getAxisConfiguration();
xConfig.addServiceGroup(serviceGroup);
server.start();

} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}

/**
* Method used to stop SimpleHTTPServer
*/
public synchronized void stopHTTPServer() {
server.stop();
}

public static void main {
startHTTPServer();
stopHTTPServer()
}
}

Monday, April 25, 2011

How to write Java code to listen runtime changes in text file

I had a requirement to write a java code to listen continues changing text file.i have used following code to do that.

public void listenServer() throws IOException {
Reader fileReader = new FileReader("/home/chamara/server.log");
BufferedReader input = new BufferedReader(fileReader);
String line = null;
while (true) {
if ((line = input.readLine()) != null) {
System.out.println(line);
continue;
}
try {
Thread.sleep(1000L);
} catch (InterruptedException x) {
Thread.currentThread().interrupt();
break;
}
}
input.close();
return isException;
}

Thursday, April 7, 2011

How to start simple HTTP server

Few days ago i had a requirement to host my own axis2 web service to the web sever.to that
How to run maven build without test
This is simple one line command.go to the build directory from command prompt and type following command.
mvn clean install -Dmaven.test.skip=true