Thursday, April 21, 2016

Create Web Service (JAX-WS), Web Service Client and call the Web Service Using Client

Some Basics:

1. Web Services contained in Application Server provides services through XML/JSON using HTTP protocol.

2. JAX-WS is JAVA API for standard web services which simplifies developing web services using Java Technology.

3. Here we use WSDL XML (Web service Descripto Language) based web service. And parse WSDL to generate Web clients.

No more theoretical discussion here. There are huge theoretical discussions on Web. 

Now we are concentrating on CRASHING hands on. Our steps are as follows:

1. Our tools are: NetBeans 7, GlassFish Server 3.1.2, JAVA 6. [i.e. you can use later versions]

2. Create a JAVA Web Application. [i.e. you can use your existing web Application

3. Create Web Service.

4. Adding Operations to the Web Service.

5. Deploying and Testing the Web Service.

6. Create Client for Web Service.

7. Use the Client to call web service.




1. Setting Up Environment


Download NetBeans from https://netbeans.org/downloads/ 

[NetBeans have aplication Server Tomcat & Glassfish.]

Check Servers from services tab in NetBeans. Here GlassFish & Tomcat are default servers. And I

manually added JBOSS for a different projects.



My Project is based on Glassfish. If you use GlassFish, then do the following otherwise ignore. In Tomcat you can call by client BUT can't test using Net Beans utility.

  • Goto  GLASSFISH_INSTALL/glassfish/domains/domain1/config/domain.xml
  •  Add the following JVM option (in bold). And Restart GlassFish to take effect.
</java-config>
  ...
  <jvm-options>-Djavax.xml.accessExternalSchema=all</jvm-options>
</java-config>

The above modification you can do easily. You can see the path from the properties of GlassFish under  Services tab.


Go to the highlighted path and open domain1/config/domail.xml. Add the above line in appropriate place seen in figure.





Restart the server if it is already run.







2. Create JAVA WEB APPLICATION

You can ignore this step if you already have any JAVA Web Application


1. Click File - New Project (Ctrl + Shift + N) or click New Project on Tool Bar.


 OR


2. Select Java Web under Category and Web Application under Projects and click Next.



3.  Give Project Name to MyWebApp and keep the all other settings as default and click Next.




4. Select GlassFish Server and Java EE 6 Web and click Finish.




5. Following files will be created in Project Explorer. Index.jsp file contained Hello World content to check the web application.







6. Now check the web application by Run.







3. Create Web Service



1. Right click MyWebApp node and do the following.




2. Give name as MyWebService and package name as com.weservice as the following and click Finish. Don't use stateless session bean i.e. that is a huge topic which i'll cover in another few posts.





3. Now MyWebService.java under package com.webservice is added under Source Packages Node. And a new Web Services Node is added. Under this node, MyWebService node is added followed by hello:String node. hello is a default method created by Net Beans which return type String.




You can add as many possible web services in your web application by following the above three steps.


4. Adding Operations to the Web Service


1. To add a new operation, open MyWEbService.java and go to Design view. And click Add Operation.




















Or right click MyWebService under Web Services node and choose Add Operation..





2. Here I create an operation Addition which return type is Integer and it has 3 parameters of type int. So I've done the following and click OK.


Now if you expand the MyWebService node under Web Services node, then you'll see the operations list in your web service MyWebService.




Or if you go Design view of MyWebService.java under Source Packages, then you'll see the pictorial view of all your methods/operations.



3. Now go to Source view of MyWebService.java under Source Packages, I've only modified the return statements of my newly created Addition operation/method. i.e. 


return num1 + num2 + num3;



@WebService annotation, the service name specified explicitly. In later version or in JBOSS or GlassFish server, the annotation can be like
                                               @WebService()
                                                @Stateless()

@WebMethod annotation specifies the the operation name in WSDL for clints.

@WebParam annotation defines mapping between parameters of operations of Web Services and WSDL entry.



5. Deploying and Testing the Web Service


1. Right click the project and choose Deploy. The IDE starts the application server, build the application and deploys it to the server. You can see the specific server log in output window.



2. Right click on MyWebService under Web Service node and choose Test Web Service





Give 1 2 3 in respective 3 text boxes and click Addition button, you'll see the following output. Or it can show exception if you give wrong input.



Now click Back button of browser. Click on WSDL File link. You'll show the following




Or you may go to http://localhost:8084/MyWebApp/MyWebService to see the web service details.



So your WSDL link is:

http://localhost:8084/MyWebApp/MyWebService?wsdl

For using your web service from remote host, replace localhost with IP Address.

http://10.80.190.60:8084/MyWebApp/MyWebService?wsdl

Now i'll use this WSDL link to create a web service client.




6. Create client for Web Service

You can create client from Net Beans. BUT i'll create client by universal way.


1. Go to cmd and issue command

wsimport -keep -p com.mywebservice.client http://10.80.190.60:8084/MyWebApp/MyWebService?wsdl

  • -keep option keeps the source files.
  • -p option specifies target package
  • com.mywebservice.client is the target package.
  • http://10.80.190.60:8084/MyWebApp/MyWebService?wsdl is the location of wsdl.










The java and class files will be located in com/mywebservice/client under the current directory. [i.e. C:\Users\khan.ashik]

2. 8 Java and corresponding 8 class files are created in that location.

3. 2 important Files are: MyWebService_Service.java class and MyWebService.java interface.

MyWebService.java is a java interface contains methods of the web service that a client can call.

MyWebService_Service.java is a java class that has URI and port of the web service and gives instance of MyWebService by getMyWebServicePort() method. So that client can call the method of MyWebService. We'll see this in next section.


7. Use the client to call web service


We use the client [discussed in previous section] JAVA files to call our web service. You can call web service from anywhere by using client JAVA files. Just do the following [I use simplest technique]:


1. Click File - New Project (Ctrl + Shift + N) or click New Project on Tool Bar. Select Java under Category and Java Application under Projects and click Next



2. Give project name to WebServiceCall and click Finish.




3. Following files will be in Project Explorer.





4. Create a package com.mywebservice.client in following way. Because I've created web service client under this package name by using wsimport command in previous section.








5. Now go to our web service client JAVA files location. In my case, it is C:\Users\khan.ashik\com\mywebservice\client

And copy all .java files (here 8 java files) and paste to newly created package in net beans.






6. The Projects explorer looks like the following





7. Double click on WebServiceCall.java to open the source code. And I've modified main function as follows.


public static void main(String[] args) {
        // TODO code application logic here
        
        // initialize the port
        MyWebService_Service port = new MyWebService_Service();
        
        
        //initialize the MyService interface to call the operations
        MyWebService service = port.getMyWebServicePort();  
        
        // call the Addition operation with 3 int parameters and retrieves the result in a variable
        int result = service.addition(1, 2, 3);
        
        //print the result of web service
        System.out.println(result);      
        
        
    }




8. Now right click on WebServiceCall.java  and choose Run File. Or choose SHift+F6 to view the output.





9. The output should be 6.




 And that's all about this topic.



Tuesday, April 12, 2016

Oracle SQL Sub Query Analysis

A sub query embedded in outer query with brackets.

For example:







Limitations:
  • Oracle allows up to 255 levels sub queries in where clause.
  • Oracle allows unlimited number of sub queries in from clause.


There are three broad divisions of sub query:
  • Single-row sub queries
  • Multiple-row sub queries
  • Correlated sub queries

Single- and Multiple-Row Sub queries

The single-row sub query returns one row. A special case is the scalar sub query, which returns a single row with one column. 
On the other hand, Multiple-row sub queries return sets of rows. 

The flow of execution for both single-row and multiple-row sub queries:

1. Sub query is evaluated at once, before evaluating the outer query.

2. Then outer query will run.

3. Check each row of outer query with the result[s] of sub query for eligible the row in the output.

For Example:

we'll use EMP table in which following data exist:

select * from emp;



Now we will query the employees who gets salary less than average:

select * from emp where salary < (select avg(salary) from emp);



In the above query, the inner query produce the average salary. And the outer query checks each row salary of EMP table whether it is less than the inner query result.

The comparison operator for a single-row sub query:

Symbol
      Meaning
=
 equal
>
greater than
>=
greater than or equal
<
less than
<=
less than or equal
<>
not equal
!=
not equal


If your sub query returns multiple rows and you are using above operators then following error occurred: 

ORA-01427: single-row subquery returns more than one row

select * from emp where salary < (select avg(salary) from emp where empid in (1,10));






The comparison operator for a multiple-row sub query:

Symbol
Meaning
IN
equal to any member in a list
NOT IN
not equal to any member in a list
ANY
returns rows that match any value on a list
ALL
returns rows that match all the values in a list


For example:

select * from emp where salary = any (select salary from emp WHERE salary>500 );


In the above query, the inner query produce the multiple salary results which are greater than 500. And after that outer query checks each row salary of EMP table whether exists in the list of sub query results.



Correlated Sub queries

If a sub query references columns in the parent query, then its result will be dependent on the parent query. This makes it impossible to evaluate the sub query before evaluating the parent query. 

We analyze correlated sub query on EMP table having following data:



  
EMPID
SALARY
1
1
200
2
10
150
3
15
10000
4
6
11100
5
4
12000
6
5
5000
7
20
2000
8
30
200
9
40
200
Now we concentrate on the below complex sub query:

select * from
emp a
where 2 = (select count(distinct(b.salary)) from emp b where b.salary<=a.salary);

Can you imagine?  The logic behind the sub query is Each row scans whole table check how many distinct values are lower than or equal this value and its is 2 or not. That means it'll produce rows which have 2nd lowest salary.

Now we analyze this. The correlated sub query will be executed as follows:
  1. Start at the first row of EMP table of outer query.
  2. Read the current row salary of EMP table.
  3. Run the sub query with the salary from step 2.
  4. Compare the result of step 3 with the value 2.
  5. Advances to the next row of EMP table.
  6. Repeat from Step 2.
Now we simulate the data of EMP table for the the above 


Outer Query
Cond.
Sub query Result
Sub query Cond.
Sub Query

A



B

Outer Query
  
EMPID
SALARY
1
1
200
2
10
150
3
15
10000
4
6
11100
5
4
12000
6
5
5000
7
20
2000
8
30
200
9
40
200
2 =
Count
(
distinct(salary)
)
B.SALARY <= A.SALARY
Inner Query
  
EMPID
SALARY
1
1
200
2
10
150
3
15
10000
4
6
11100
5
4
12000
6
5
5000
7
20
2000
8
30
200
9
40
200







Rows
Current Row Salary
Cond.
Sub query Result
Eligible?
1
200
2=
2
[10000,11100,12000,150,200,2000,5000] <= 200
Y
2
150
2=
1
[10000,11100,12000,150,200,2000,5000] <=150
N
3
10000
2=
5
[10000,11100,12000,150,200,2000,5000] <= 10000
N
4
11100
2=
6
[10000,11100,12000,150,200,2000,5000] <= 11100
N
5
12000
2=
7
[10000,11100,12000,150,200,2000,5000] <= 12000
N
6
5000
2=
4
[10000,11100,12000,150,200,2000,5000] <= 5000
N
7
2000
2=
3
[10000,11100,12000,150,200,2000,5000] <= 2000
N
8
2000
2=
3
[10000,11100,12000,150,200,2000,5000] <= 2000
N
9
200
2=
2
[10000,11100,12000,150,200,2000,5000] <=200
Y