Wednesday, February 29, 2012

Rest with JAX-RS using Jersey


This article explains how to develop RESTful web services in Java with the JAX-RS reference implementation Jersey.
In this article Eclipse (Helios), Java 1.6, GlassFish 3.0.1 and JAX-RS 1.1. (Jersey 1.5) is used. 

Introduction

  • JAX-RS stands for Java API for XML RESTful Services
  • A RESTFul webservices is based on the HTTP methods
  • In a REST based architecture everything is a resource. 
  • It typically defines the base URI for the services, the MIME-types its supports (XML, Text, JSON, user-defined,..) and the set of operations (POST, GET, PUT, DELETE) which are supported. 
  • JAX-RS supports the creation of XML and JSON via JAXB
  • JAXB stands for Java Architecture for XML Binding
  •  (JAXB) is a Java standard that defines how Java objects are converted to/from XML
  • @XmlRootElement Define the root element for a XML tree
CRUD/HTTP mapping
Application task HTTP command
Create POST
Read GET
Update PUT
Delete DELETE

Example

1) Create Dynamic Web Project
2)  To start working with Jersy you need to put the needed JARs in the lib folder, as we are using GlassFish we don't need this step as it is already there
If it's not there, you can install it on GlassFish

References
http://jonas.ow2.org/JONAS_5_2_0_M4/doc/doc-en/html/howto_use_jaxrs.html 
http://www.myeclipseide.com/documentation/quickstarts/webservices_rest/
http://www.vogella.de/articles/REST/article.html
https://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with 
http://docs.oracle.com/cd/E19776-01/820-4867/ggnzk/index.html

Tuesday, February 21, 2012

EJB Concepts

Stateful - is used if you want to have multiple operations & specific to single client
(One stateful instance for a client) 


Stateless - container create a pool of instances & forward it when a client request comes, does not matter who is the client
(One instance can serve multiple clients) 



Local interface - used if EJB will be accessed locally (same env)
If clients and EJB reside in the same JVM

Remote interface - if your ejb will be used remotely (in different env)
If clients and EJB does not reside in the same JVM


-You use EJB when and only when you need the container services. That is, transaction, persistence and security.
- If you are using Stateless session beans you are scalable because you are stateless.
- While we are at it lets address another common misconception. Lifecycle management. It is often claimed that EJB gives you some necessary lifecycle management and object pooling. It also is supposed to help you in a multi threaded environment. The trouble with this claim is that if you are stateless there are no threading issues and you only need one instance to handle all of your clients on a server. Servlets and stateless Session beans are essentially equivalent ( keep in mind that HTTPServlets are only one type of Servlet). In the world of Servlets the spec allowed you to either create a new Servlet for every client or to use one and make it thread safe. Every servlet container does it the second way and yet EJB only allows containers to do it the first way.
- Performance comes from a good design not from using the container provided services.
- EJB3 over spring because it is a standard, and because it enforce modularity (to certain degree). It's possible to write an EJB once and have it reused by multiple system, regardless of the architecture of the system.

Creating a stateless EJB 3 for GlassFish Using Eclipse and accessing it remotely

This Post is about  creating a remote stateless EJB3 using eclipse helios and deploying it on GlassFish 3 and accessing it from another machine using a Java client

Machine1 <192.168.1.69>
1) Create a new EJB Project <SessionBeanProject>
2) Accept the defaults and press next
3) Make sure that the "EJB Client JAR" check box is not checked, we are going to create our interface manually and press finish
4) Now we are going to create the bean class, right click on your project and choose new Session Bean <CalcSessionBean>
5) Configuration of this bean is stateless and remote, so that it creates automatically the remote interface
6) Add your methods and implementation in the bean class

7) Copy your methods signatures and put it in the remote interface class

What is the base role of the interface?
It will be used in the remote client when we call this bean, and the client has to know what methods are available
The remote interface behaves as an API for the EJB that is used by clients to communicate with the EJB.

Deploy this stateless bean
1) Start GlassFish 3 server
2) Add your EJB project to the server
3) You can see "StatelessBeanProject synchronized", so we know it's deployed
4) View the server log by right clicking on the server - GlassFish Enterprise Server - View Log File
You can see the following
INFO: Portable JNDI names for EJB CalcSessionBean : [java:global/SessionBeanProject/CalcSessionBean, java:global/SessionBeanProject/CalcSessionBean!calc.CalcSessionBeanRemote]
5) This JNDI name will be used by the remote client to lookup the bean
6) There is another Non-Portable JNDI names, but we are going to use the Portable one
7) If you went to the Admin console - Applications - you can see your Bean, so we know the bean is deployed


Call the deployed stateless bean methods from a remote client
Machine2 <192.168.1.67> 
1) Create a regular Java Project <StatelessBeanClientJavaProject>
2) Add GF server JARs
3) Add GF Client libraries to the project (javaee.jar and appserv-rt.jar)
4) Copy your Remote interface in the project
5) Lookup the bean and call its method from the main method
By placing the Host and Port, we are referencing the machine where the bean is deployed and GlassFish is running. Otherwise the client will try to access localhost and it will fail
port 3700 is the default port for IIOP

Local Beans can not be called from remote clients that are on a different JVM