Friday, March 9, 2012

How to Connect Glassfish Derby from Eclipse IDE

Below is the steps to connect GlassFish v3.0.1 Embedded Derby database from Eclipse Helios

1) Start up the Derby Engine
Either by configuring it to start with the GlassFish server startup
 






Or starting Derby in server mode
aOpen cmd and go to the Derby setup bin directory
startNetworkServer

1) From IDE Window menu,  select Open Perspective > Other > choose Database Development


2) On the Database Development panel (figure below), righ-click "Database Connections" > select New from the menu to create a New Connection Profile and choose 'Derby' from the list and provide connection name and press next
3) Insert connection details
4) Press Test Connection, you should see Ping Succeeded msg
5) Press Finish

Tuesday, March 6, 2012

Maven

To create a project
mvn archetype:generate -DgroupId=com.vis.rest -DartifactId=MobileSpoc.rest -DarchetypeArtifactId=maven-archetype-quickstart  -DinteractiveMode=false


Creates a simple project named "MobileSpoc.rest" and a package named "com.vis.app"

If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete. Don't worry, there are ways to fix that.

pom.xml
The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want.

Changing Target Java Version
1) By default, Maven attempts to build for compatibility with JDK 1.3 
2) Using Jersey requires annotations, which aren't supported until JDK 1.5
3) Maven must be instructed to target a different JDK version via the POM file

Build the project
 mvn package
Unlike the first command executed (archetype:generate) you may notice 
the second is simply a single word - package. Rather than a goal, this is a phase.
A phase is a step in the build lifecycle,
 which is an ordered sequence of phases. When a phase is given, Maven 
will execute every phase in the sequence up to and including the one 
defined. For example, if we execute the compile phase, the phases that actually get executed are:
  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile
You may test the newly compiled and packaged JAR with the following command:
java -cp target/my-app-1.0-SNAPSHOT.jar com.vis.app.App

 
Maven Phases
  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Creating a sample Java EE 6 Blog Application with JPA, EJB, CDI, JSF and Primefaces on GlassFish

http://www.hascode.com/2011/02/creating-a-sample-java-ee-6-blog-application-with-jpa-ejb-cdi-jsf-and-primefaces-on-glassfish/ 

  
 
References
http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html 

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