Post on 07-Apr-2018
8/3/2019 4 Jee Pre Ejb3
1/42
Java Enterprise Edition
Pre Enterprise Java Beans 3
Annotations
RMI
8/3/2019 4 Jee Pre Ejb3
2/42
Annotations
8/3/2019 4 Jee Pre Ejb3
3/42
Java Annotations Annotations in simple terms are metadata / markers which
provide extra information to compiler or run time
environments.
Suppose you write a Servlet, how does web container
knows which class represent Servlet. Before JavaSE5 there was a trend of using XML's to
provide the meta data of a program.
That resulted in reading a configuration file and the source
code to understand the program.
At runtime also, the tools have to read both the XML file
and the class file to execute it correctly.
8/3/2019 4 Jee Pre Ejb3
4/42
Java Annotations.
With annotations an attempt is made to bring themetadata inside the source code itself.
It increases the readability of the program also.
It will start inspecting the class files and will look for amarker in the class file @WebServlet.
In early JEE5 the same thing was done by reading
web.xml.
What annotations have done here is to move the
configuration from XML file to the class file itself.
8/3/2019 4 Jee Pre Ejb3
5/42
Annotations are applied to
package declarations,
type declarations,
constructors,
methods,
fields, parameters, and
variables.
8/3/2019 4 Jee Pre Ejb3
6/42
Usage
An annotation begins with symbol @. Annotations have a number of uses, among them:
Information for the compiler - Annotations can be used by
the compiler to detect errors or suppress warnings
Compiler-time and deployment-time processing -
Software tools can process annotation information to
generate code, XML files, and so forth
Runtime processing - Some annotations are available to beexamined at runtime (reflection)
8/3/2019 4 Jee Pre Ejb3
7/42
Built-in Annotations
Simple Annotations @Override
@Deprecated
@SupressWarnings
Meta-Annotations
@Target
@Retention
@Inherited
8/3/2019 4 Jee Pre Ejb3
8/42
Annotations Used by the Compiler
@Deprecated- indicates that the marked element isdeprecated and should no longer be used
@Override- informs the compiler that the element is meant
to override an element declared in a superclass
@SuppressWarnings- tells the compiler to suppressspecific warnings that it would otherwise generate
8/3/2019 4 Jee Pre Ejb3
9/42
Using Annotations
Syntactically, the annotation is placed in front ofthe program element's declaration, similar tostatic or final orprotected
@WebServlet(name = "FirstServlet",urlPatterns = {"/csc"})
public class MyServlet extendsHttpServlet {
An annotation instance consists of the "@" sign
the annotation name
a parenthesized list of name-value pairs
8/3/2019 4 Jee Pre Ejb3
10/42
The Deprecated Annotation Deprecated: in a computing language considered obsolete but still
available for use, though planned to be phased out.Date d1 = new Date(2010, 10, 22) [DeprecatedConstructor]
This annotation indicates that when a deprecated program element is
used, the compiler should warn you about it.First, create a class with the deprecated method as follows:public class MyClass {
@Deprecated
public void doMethod1() {System.out.println("This is Method1");
}
}
8/3/2019 4 Jee Pre Ejb3
11/42
Deprecated Annotation .
Next, try to invoke this method from another class:public class TestAnnotations {
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.doMethod1();
}
}
8/3/2019 4 Jee Pre Ejb3
12/42
Suppresswarnings Annotation
This annotation indicates that compiler warnings shouldbe shielded in the annotated element and all of its sub-
elements.
In TestAnnotations Class,@SuppressWarnings({"deprecation"})
public TestAnnotations() {
.... . .
}
8/3/2019 4 Jee Pre Ejb3
13/42
Creating Annotations
Similar to normal interface declarations
An at-sign @ precedes the interface keyword
Each method declaration defines an element of the annotationtype
Methods can have default values
Once an annotation type is defined, you can use it to annotatedeclarations
Method declarations should not have any parameters
Method declarations should not have any throws clauses
Return types of the method should be one of the following: primitives, String, Class, enum, array of the above types
No exceptions
No inheritance
8/3/2019 4 Jee Pre Ejb3
14/42
Annotations Contd..,
Example to Define an Annotation (Annotation type)public @interface MyAnnotation {
String doSomething();
} Example to Annotate Your Code (Annotation)
@MyAnnotation (doSomething="to do")
public void mymethod() {....
}
8/3/2019 4 Jee Pre Ejb3
15/42
Annotating Declarations
In annotations with a single element, the element should be named
value: (Single Member Annotation)
It is permissible to omit the element name and equals sign (=) in asingle-element annotation:
If no values, then no parentheses needed: (Marker Annotation)
public @interface Preliminary { }
@Preliminary public class TimeTravel { ... }
public @interface Copyright {String value();
}
@Copyright("2002 Yoyodyne Propulsion Systems")public class OscillationOverthruster { ... }
8/3/2019 4 Jee Pre Ejb3
16/42
Meta-Annotations
@Target- indicates the targeted elements of a class inwhich the annotation type will be applicable
TYPE, FIELD, METHOD, PARAMETER,
CONSTRUCTOR, etc
@Retention- Duration for which the annotationelement is kept.
SOURCE, CLASS, RUNTIME
@Documented- Annotation to be included in javadoc @Inherited- Annotations will be inherited by a
subclass
8/3/2019 4 Jee Pre Ejb3
17/42
The Target Annotation It contains the following enumerated types as its value:
@Target(ElementType.TYPE) applied to any element of a class @Target(ElementType.FIELD) applied to a field or property
@Target(ElementType.METHOD) applied to a method levelannotation
@Target(ElementType.PARAMETER) applied to the parameters ofa method
@Target(ElementType.CONSTRUCTOR) applied to constructors
@Target(ElementType.LOCAL_VARIABLE) applied to localvariables
@Target(ElementType.ANNOTATION_TYPE) indicates that thedeclared type itself is an annotation type
8/3/2019 4 Jee Pre Ejb3
18/42
The Target Annotation Example First, define an annotation named Test_Target with @Target metadata:
import java.lang.annotation.ElementType;import java.lang.annotation.Target;
@Target(ElementType.METHOD)
public @interface MyAnnotation {
String doSomething();}
Next, create a class that will use the Test_Target annotation:
public class TestAnnotations {. . . . . .
@MyAnnotation(doSomething="Hello World")
public void xy() {
System.out.println("tt");
}
private String x;
}
8/3/2019 4 Jee Pre Ejb3
19/42
The Retention Annotation The retention annotation indicates where and how long
annotations with this type are to be retained. There are
three values:
RetentionPolicy.SOURCEAnnotations with this type
will be by retained only at the source level and will beignored by the compiler
RetentionPolicy.CLASSAnnotations with this type
will be by retained by the compiler at compile time, but
will be ignored by the VM
RetentionPolicy.RUNTIMEAnnotations with this
type will be retained by the VM so they can be read only
at run-time
8/3/2019 4 Jee Pre Ejb3
20/42
Retention Example
Let's write a simple annotation called author annotation.
We can use this annotation over any class to specify who
is the author of the class.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)public @interface AuthorAnnotation{
public String authorName() default "NoAuthor";
}
Let's write a class where we apply the annotation:
@AuthorAnnotation(authorName=XYZ")
public class AnnotatedClass { }
8/3/2019 4 Jee Pre Ejb3
21/42
Retention Example
Let's write a method to process the annotation. It can bea main method:
Class clazz =Class.forName("com.demo.AnnotatedClass");
Annotation[] annotations =clazz.getAnnotations();
for(Annotation a: annotations){
AuthorAnnotation aa = (AuthorAnnotation)a;
System.out.println("Class author is "+aa.authorName());
}
8/3/2019 4 Jee Pre Ejb3
22/42
Remote Method Invocation (RMI)
8/3/2019 4 Jee Pre Ejb3
23/42
Distributed Computing
Distributed computing is the science of calling components that
exist on different physical systems over the Network.
Distributed computing in Java is achieved using Remote Method
Invocation - One JVM can invoke methods of an object in another
JVM.
The Java Remote Method Invocation (RMI) allows an object
running in one Java virtual machine to invoke methods on an
object running in another Java virtual machine.
8/3/2019 4 Jee Pre Ejb3
24/42
Distributed Computing.. A remote invocation of a method on a distributed
component follows a common process that is similar
across almost all distributed computing technologies.
8/3/2019 4 Jee Pre Ejb3
25/42
Distributed Computing.
A Remote object is always accessed via its remoteInterface. In other words the client invokes methods on
the object only after casting the reference to the remote
interface.
Stub and
Skeleton
8/3/2019 4 Jee Pre Ejb3
26/42
Stub Object
Stub is the client side object that represents or acts as aproxy for the remote object.
The stub has the list of methods, as the remote object.
Sequence of tasks performed by a stub:
Initiates a connection with the remote VM containing the
actual remote object.
Marshals the parameters to the remove VM
Waits for the result of method invocation Unmarshals the return value or exception returned
Returns the value to the caller.
8/3/2019 4 Jee Pre Ejb3
27/42
Skeleton Object
On the server side, the skeleton object takes care of allof the details of Remoteness so that the remote object
doesn't need to worry about them.
Sequence of tasks performed by a skeleton
Unmarshals the parameters for the remote method.
Invokes the method on the actual remote object
implementation
Marshals the result or exception to the caller.
8/3/2019 4 Jee Pre Ejb3
28/42
The General RMI Architecture
The server must first bindits name to the registry
The client lookup the
server name in the registry
to establish remotereferences.
The Stub serializing the
parameters to skeleton, the
skeleton invoking the
remote method and
serializing the result back
to the stub.
RMI Server
skeleton
stub
RMI Client
Registry
bind
lookupreturn call
Local Machine
Remote Machine
8/3/2019 4 Jee Pre Ejb3
29/42
Steps for Developing an RMI System
1. Define remote interface2. Develop remote object by implementing the remote interface.
3. Develop Server program.
4. Compile Java source files.
5. Generate client stubs and server skeletons.
6. Start RMI registry.
7. Start remote server objects.
8. Develop client program.
9. Run client
8/3/2019 4 Jee Pre Ejb3
30/42
Step 1: Defining the Remote Interface
java.rmi.Remoteused to mark object as being remote. Remote Interface must be declared public.
It must extend the java.rmi.Remote Interface.
Each method in the Interface has to throw
java.rmi.RemoteException
/* SumRemote.java */
import java.rmi.*;
public interface SumRemote extends Remote
{
public int sum(int a,int b) throws RemoteException;
}
8/3/2019 4 Jee Pre Ejb3
31/42
Step 2: Develop the remote object and its interface
The server is a simple unicast remote server.
Create server by extendingjava.rmi.server.UnicastRemoteObject .
The server uses the RMISecurityManager to protect its resourceswhile engaging in remote communication.
/* SumRemoteImpl.java */import java.rmi.*;import java.rmi.server.*;
public class SumRemoteImpl extends UnicastRemoteObjectimplements SumRemote {SumRemoteImpl() throws RemoteException {
super();}
public int sum(int a,int b) throws RemoteException {return a + b;
}
}
8/3/2019 4 Jee Pre Ejb3
32/42
Step 3: Server Program
The server must bind its name to the registry, theclient will look up the server name.
Use java.rmi.Naming class to bind the
server name to registry. In this example the name call
SERVERObject1.
In the main method of your server object, theRMI security manager is created and installed.
8/3/2019 4 Jee Pre Ejb3
33/42
Step 3: Server Contd..,/* SampleServer.java */
import java.rmi.*;public class SampleServer {public static void main(String[] args) {try {
SumRemoteImpl Obj1 = new SumRemoteImpl();Naming.rebind(SERVERObject1", Obj1);System.out.println(Object Binded...");}catch(Exception e) {System.out.println("Error..." + e);
}}}Step 4: Compile the java source files.
8/3/2019 4 Jee Pre Ejb3
34/42
Step 5: Generate Stub and Skeleton
The RMI system provides an RMI compiler (rmic) thattakes your generated interface class and procedures stub
code on its self.
rmic -vcompat SumRemoteImpl
8/3/2019 4 Jee Pre Ejb3
35/42
Step 6: Start the RMI registry
The RMI applications need install to Registry. And theRegistry must start manual by call rmiregisty.
The rmiregistry us uses port 1099 by default.
You can also bind rmiregistry to a different port by
indicating the new port number as : rmiregistry
> start rmiregistry
Step 7:
Start the Server
> java SampleServer
8/3/2019 4 Jee Pre Ejb3
36/42
Step 8: Develop the client program
import java.rmi.Naming;
public class SampleClient {public static void main(String[] args) {
try {
String url = "rmi://localhost/ SERVERObject1";
SumRemote remoteObject = (SumRemote)Naming.lookup(url);
System.out.println("Got remote object");
System.out.println(" 10 + 20 = " +remoteObject.sum(10,20) );
}
catch (Exception e) {System.out.println("Error " + e);
}
}
}
Step 9: Run the Client
8/3/2019 4 Jee Pre Ejb3
37/42
Distributed Applications Require Scalability: Objects can be distributed across multiple
computers while their location remain transparent toclients.
Transactions
Concurrency: Manage concurrent access to an objects,allowing only a single item at a time to use.
Security: A security model that enables security to be
implemented, without affecting the object
implementation.
Portability across different application servers is desired.
Presistence
Messaging
8/3/2019 4 Jee Pre Ejb3
38/42
Distributed Applications Require
Deploy-time customization
Different kinds of clients will access the application i.e.
Objects can be accessed in multiple ways; local or remote,
SOAP or REST web service.
Doing programmaticallywriting the code by yourself.
As application developers can focus on developing
business logic, and not worry about other enterprise
application requirements such as scalability, security,
transactions, so on.
Doing declarativelyBy an Application Server
8/3/2019 4 Jee Pre Ejb3
39/42
Application Server
Servers Support JEE JBoss 5
Glassfish 2.1
BEA WebLogic 10 Oracle AS 11
IBM WebSphere 7
Apache Geronimo 2 SAP NetWeaver Java EE 5 Edition
TmaxSoft JEUS 6
8/3/2019 4 Jee Pre Ejb3
40/42
Starting the JBoss Server
Our first step is to try running the server.
You'll find abin directory inside the main JBossdirectory which contains various scripts.
Execute the run script run.bat.
The last message (obviously with different valuesfor the time and start-up speed) should look likethe following.
12:31:23,996 INFO [Server] JBoss (MXMicroKernel) [4.0.2 (build:CVSTag=JBoss_4_0_2 date=200504191712)]Started in 47s:608ms
8/3/2019 4 Jee Pre Ejb3
41/42
Verifying the Server is running / not
You can verify that the server is running by goingthe JBoss web server, which is running on port8080.
The default page has links to a few useful JBoss
resources. To make sure, go to http://localhost:8080/in your
browser. You should see the JBoss welcome page
To Stop JBoss Server
To stop the server, you can type Ctrl-C
or you can execute the shutdown script shutdown.batfrom thebin directory.
http://localhost:8080/http://localhost:8080/8/3/2019 4 Jee Pre Ejb3
42/42
Integrating NetBeans with JBoss First, we need to click on Window | Services.
Next, we need to right-click on the Servers in the tree
inside the Services window, and select Add Server...
from the resulting pop up menu.
Then we need to select the server to install from the list inthe resulting window, and click on the button labeled
Next>.
We then need to enter a location in the file system where
the application server is installed and clickNext>.
Finally, we need to select a domain, host, and port for our
application server, and then click on the Finish button.