Wednesday, February 20, 2013

How to compare two Dates ?


Use Case : How to compare two java.util.Date values ?
Implementation :

// Below method adds number of days to the given date based on provided time zone and return the result Date value


    public static Date addDays(Date pDate, int pDays, TimeZone pTimeZone) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(pDate);
        cal.add(Calendar.DAY_OF_MONTH, pDays);
        cal.setTimeZone(pTimeZone);
        return cal.getTime();
    }


    public static boolean before(Date pFromDate, Date pToDate) {
        return pFromDate.before(pToDate);
    }






// Compare the date

Date date1 = new Date() // Current Date
Date date2 = addDays(date1,365,Timezone.getDefaultTimezone()); // 1 year from now
Date endDate // User entered data

boolean result = before(date2,endDate); // true - if date2 is before end date otherwise false.






Monday, February 18, 2013

How to create Webservice proxy using Jdeveloper


Use case : How to create web service proxy using Jdeveloper 11g

Implementation : In my previous blog, I have outlined how to create a web service using Jdeveloper, In this blog I would like to explain about how to create a web service proxy using Jdeveloper.  As you all know that web service can be invoked using SOAP protocol and exchanges request / reply messages using XML , There may be a requirement to invoke a webservice in java layer, one alternative to accomplish this requirement is to create a web service proxy, what does this means is , Jdeveloper tool will generate Java library/class files based upon web service WSDL url and these java classes can be accessible anywhere in java layer.

Steps :

In this example, here is my WSDL url, Make sure this web service works fine by opening in any browser.

http://localhost:7101/WebserviceProxy-MathUtilWSProject-context-root/MathUtilWSPort?wsdl

- Create a new project with 'Java & Webservice' technologies as shown below
- Right click on newly created project, Choose New , Choose Webservices under Business tier -> Choose Webservice proxy as shown below

- Choose JAX-WS Style, If the web service is type of Jax-ws
-  Paste the WSDL URL for which you want to create a web service proxy as shown below
- Give the proper Package name as well as  types as shown in below screen shot
- Follow rest of steps leaving default options , Finally web service proxy will be created.
- Below is the screen shot shows how to test newly created web service proxy




Saturday, February 16, 2013

How to Create, Test Webservice using Jdeveloper

Usecase : How to create & test a webservice using Jdeveloper

What is Web Service ?

A web service is a function that can be accessed by other programs over the web (Http).

Webservices are two types as follows 

1. SOAP(Simple Object Access Protocol) Webservice - SOAP Web Services are standard-based and supported by almost every software platform: They rely heavily in XML and have support for transactions, security, asynchronous messages and many other issues. It’s a pretty big and complicated standard, but covers almost every messaging situation. On the other side.

2. REST Webservice -

Representational State Transfer (REST) is an architectural style that specifies constraints, such as the uniform interface, that if applied to a web service induce desirable properties, such as performance, scalability, and modifiability, that enable services to work best on the Web.

RESTful services relies of HTTP protocol and verbs (GET, POST, PUT, DELETE) to interchange messages in any format, preferable JSON and XML. It’s a pretty simple and elegant architectural approach.
As in every topic in the Java World, there are several libraries to build/consume Web Services. In the SOAP Side you have the JAX-WS standard and Apache Axis, and in REST you can use Restlets or Spring REST Facilities among other libraries.

RESTful Services are appropriate in this scenarios:

If you have limited bandwidth
If your operations are stateless: No information is preserved from one invocation to the next one, and each request is treated independently.
If your clients require caching.

SOAP Services are appropriate in below scenarios

If you require asynchronous processing
If you need formal contract/Interfaces
In your service operations are stateful: For example, you store information/data on a request and use that stored data on the next one.

Implementation :

 - Create a simple Java class with a method called sayHello() as shown in below screenshot


- Right click on Java class, which need to be generated as web service, Choose 'Create Webservice' option

- Give web service name and port name

Choose SOAP binding version

Select the method want to expose in web service, Follow Next, Next..

 - Once the web service is created, Right click on web service and choose 'Test Web Service' option

- Test the web service as shown in below screen shot.



- Here is the webservice URL, and this can be consumed by any client using SOAP protocol.


http://localhost:7101/HelloWorldWebservice-Model-context-root/HelloWorldWSPort?wsdl

For More information about weservices 



Saturday, February 9, 2013

Java Naming standards


It is very very important to follow naming/coding standards while developing java classes, Below is the link to know java naming standards.

http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html



Monday, February 4, 2013


Subject : How to add an element to List while initializing a List in Java

Solution :
  List<Integer> idList = Arrays.asList(121,121,3434);
  List<String> nameList = Arrays.asList("Ravi","Bob");

As you all knew, List is an interface and ArrayList is a class that implements List interface. It is best practice to create an instance for ArrayList using List Interface as shown in below example

List<String> strList = new ArrayList<String>();

How to add elements to a List while initializing in Java

Subject : How to add an element to List while initializing a List in Java

Solution :
  List<Integer> idList = Arrays.asList(121,121,3434);
  List<String> nameList = Arrays.asList("Ravi","Bob");

As you all knew, List is an interface and ArrayList is a class that implements List interface. It is best practice to create an instance for ArrayList using List Interface as shown in below example

List<String> strList = new ArrayList<String>();