Friday, June 28, 2013

Creating a simple webservice using JAX-WS

This example creates a simple webservice using JAX-WS



Message.java 

package edu.demo.ws;

importjavax.jws.WebMethod;
importjavax.jws.WebService;

@WebService
public class Message {

      @WebMethod
      public String getMessage(String text) {
            return "Message " + text;
      }
}

Main.java


package edu.demo.ws;

importjavax.xml.ws.Endpoint;

public class Main {

      public static void main(String[] args) {
            Endpoint.publish("http://localhost:8181/message", new Message());

      }

}

Run the Main.java as Java Application and in the browser open the url  http://localhost:8181/message?wsdl



<?xml version="1.0" encoding="UTF-8" ?>
- <!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
 -->
- <!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
 -->
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.demo.edu/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.demo.edu/" name="MessageService">
- <types>
- <xsd:schema>
 <xsd:import namespace="http://ws.demo.edu/" schemaLocation="http://localhost:8181/message?xsd=1" />
 </xsd:schema>
 </types>
- <message name="getMessage">
 <part name="parameters" element="tns:getMessage" />
 </message>
- <message name="getMessageResponse">
 <part name="parameters" element="tns:getMessageResponse" />
 </message>
- <portType name="Message">
- <operation name="getMessage">
 <input message="tns:getMessage" />
 <output message="tns:getMessageResponse" />
 </operation>
 </portType>
- <binding name="MessagePortBinding" type="tns:Message">
 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="getMessage">
 <soap:operation soapAction="" />
- <input>
 <soap:body use="literal" />
 </input>
- <output>
 <soap:body use="literal" />
 </output>
 </operation>
 </binding>
- <service name="MessageService">
- <port name="MessagePort" binding="tns:MessagePortBinding">
 <soap:address location="http://localhost:8181/message" />
 </port>
 </service>
 </definitions>

  Use the URL in the above wsdl under SchemaLocation http://localhost:8181/message?xsd=1



 <?xml version="1.0" encoding="UTF-8" ?>
- <!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
 -->
- <xs:schema xmlns:tns="http://ws.demo.edu/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://ws.demo.edu/">
 <xs:element name="getMessage" type="tns:getMessage" />
 <xs:element name="getMessageResponse" type="tns:getMessageResponse" />
- <xs:complexType name="getMessage">
- <xs:sequence>
 <xs:element name="arg0" type="xs:string" minOccurs="0" />
 </xs:sequence>
 </xs:complexType>
- <xs:complexType name="getMessageResponse">
- <xs:sequence>
 <xs:element name="return" type="xs:string" minOccurs="0" />
 </xs:sequence>
 </xs:complexType>
 </xs:schema>


Use SoapUI or Webservices Explorer in eclipse, post the Wsdl link and you should be able to use your first webservice

Wednesday, October 24, 2012

Create new file depending on the last modified time



The following script would automatically create .trg files if a file doesn’t exist. It also checks for the file last modified time. Here I have placed it as 5 minutes. Only if the file has not been modified for last 5 minutes it goes and creates the .trg file. The timing can be extended as needed. This should prevent the script from creating .trg files for the files that are getting processed. 


#!/bin/bash

cd /hulanas/smartdoc/inbound

for file in *.xml
do
# check if .trg file already exists, if yes ignore
  if [ -a $file.trg ]
    then
       echo "$file.trg already exists"
# check if the file has not been modified for more than 5minutes (time can be increased depending on the requirement)
# if yes, go ahead and create the .trg file.
# This is to prevent .trg files from being created for the files that 
# might be getting processed.
  elif [ `find $file -type f -mmin +5` ]
     then
# create the .trg file
        touch $file.trg
  fi
done

Friday, September 7, 2012

Reading portion of a huge log file

If you have a several hundred MB log file, how do you quickly find a portion of it you're interested in? Your vi may not be able to open the file, complaining file too large, and/or take too long and use too much memory. Here's what you can do.

Let's say you want Mon Jul 15's data from July's access_log, here's the command
sed -n '/^Mon Jul 15/p; /^Tue Jul 16/q' access_log > /tmp/qq 
Without the quit command, sed would scan the file to the end or till you press ^C. Note the undocumented semicolon that allows you to put two sed commands on one line. (By the way, on Solaris creating a file under /tmp may be faster because tmpfs should be memory-based, unless you're short on RAM. But remember to delete big files under /tmp when you're done because they reduce available swap space.)

Saturday, April 28, 2012

OutOfMemoryError vs StackOverflowError

OutOfMemoryError:
Thrown when Java is running out of memory and cannot allocate any more memory in the heap for the object

StackOverflowError:
The stack space lives at the top-end of the address space and the heap lives at the bottom-end of the address space. As stack grows, it grows downwards. As heap grows, it grows upwards. So there is a potential that the two can collide and when that happens, you get a StackOverflowError thrown. Typically happens when you call some methods recursively (that keeps going-and-going).


Sunday, April 15, 2012

Command Line Arguments


packageorg.training.fundementals;

public class Args {

      /**
       * @param args
       */
      public static void main(String[] args) {
     
            System.out.println(args[0]);
            System.out.println(args[1]);
            System.out.println(args[2]);
            System.out.println(args[3]);
System.out.println(args[3]);
System.out.println(args[3]);

           

      }

}

C:\> java Args This is a simple Java program
Output:
This
is
a
simple
Java
Program


Saturday, February 25, 2012

Running Multiple Instances of Tomcat on Windows Machine

To run multiple instances of tomcat the two most important things that you need are CATALINA_HOME and CATALINA_BASE variables set properly.

CATALINA_HOME refers to the base tomcat directory which has all the files required for starting and stopping tomcat. It mainly includes two folders bin and lib.

CATALINA_HOME  would be common for all the tomcat instances running on that machine.

CATALINA_BASE would be different for different instances and each instance would refer to its particular CATALINA_BASE directory. This usually includes the following folders inside it, conf, webapps, temp, work & log.

Inside the conf folder, edit the server.xml file to listen to different ports for different instances.

How to have multiple CATALINA_BASE variables ?

Download and copy the tomcat folder to some directory of your choice. I have installed it like this F:\Servers\apache-tomcat-7.0.26

Now create another folder and inside that create a folder name bin. Inside bin create two files namely startup.bat and shutdown.bat

I created a folder name Elephant (which is one of the name of my tomcat instance) under F:\Servers\

Now in the startup.bat put the following lines and edit the path as appropriate.


set CATALINA_BASE=F:\Servers\Elephant
set CATALINA_HOME=F:\Servers\apache-tomcat-7.0.26
F:\Servers\apache-tomcat-7.0.26\bin\startup.bat

In shutdown.bat put the following:


set CATALINA_BASE=F:\Servers\Elephant
set CATALINA_HOME=F:\Servers\apache-tomcat-7.0.26
F:\Servers\apache-tomcat-7.0.26\bin\shutdown.bat




Thursday, December 8, 2011

NetBeans v/s Eclipse


NetBeans is like the long lost love that has aged with grace and dignity, retaining the characteristics that made you love her in the first place, whereas Eclipse is like the aging, overeating trophy wife who thinks she’s still “all that” and lets you know about it, frequently.