4 Jee Pre Ejb3
4 Jee Pre Ejb3
Annotations
RMI
Annotations
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.
Java Annotations.
With annotations an attempt is made to bring the metadata inside the source code itself. It increases the readability of the program also. It will start inspecting the class files and will look for a marker 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.
package declarations, type declarations, constructors, methods, fields, parameters, and variables.
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 be examined at runtime (reflection)
Built-in Annotations
Simple Annotations @Override @Deprecated @SupressWarnings Meta-Annotations @Target @Retention @Inherited
@Deprecated - indicates that the marked element is deprecated 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 suppress specific warnings that it would otherwise generate
Using Annotations
Syntactically, the annotation is placed in front of the program element's declaration, similar to static or final or protected
@WebServlet(name = "FirstServlet", urlPatterns = {"/csc"}) public class MyServlet extends HttpServlet { An annotation instance consists of the "@" sign the annotation name a parenthesized list of name-value pairs
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) [Deprecated Constructor]
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"); } }
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(); } }
Suppresswarnings Annotation
This annotation indicates that compiler warnings should be shielded in the annotated element and all of its subelements. In TestAnnotations Class, @SuppressWarnings({"deprecation"}) public TestAnnotations() { .... . . }
Creating Annotations
Similar to normal interface declarations An at-sign @ precedes the interface keyword Each method declaration defines an element of the annotation type Methods can have default values Once an annotation type is defined, you can use it to annotate declarations 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:
No exceptions No inheritance
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() { .... }
Annotating Declarations
In annotations with a single element, the element should be named value: (Single Member Annotation)
public @interface Copyright { String value(); }
It is permissible to omit the element name and equals sign (=) in a single-element annotation:
@Copyright("2002 Yoyodyne Propulsion Systems") public class OscillationOverthruster { ... }
Meta-Annotations
@Target - indicates the targeted elements of a class in which the annotation type will be applicable
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 level annotation @Target(ElementType.PARAMETER) applied to the parameters of a method @Target(ElementType.CONSTRUCTOR) applied to constructors @Target(ElementType.LOCAL_VARIABLE) applied to local variables @Target(ElementType.ANNOTATION_TYPE) indicates that the declared type itself is an annotation type
@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; }
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 be ignored 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
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 "No Author"; }
Retention Example
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()); }
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.
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.
Distributed Computing.
A Remote object is always accessed via its remote Interface. In other words the client invokes methods on the object only after casting the reference to the remote interface.
Stub Object
Stub is the client side object that represents or acts as a proxy 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.
Skeleton Object
On the server side, the skeleton object takes care of all of 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.
The server must first bind its name to the registry The client lookup the server name in the registry to establish remote references. The Stub serializing the parameters to skeleton, the skeleton invoking the remote method and serializing the result back to the stub.
return
call
lookup
stub
RMI Client
Local Machine
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; }
The server is a simple unicast remote server. Create server by extending java.rmi.server.UnicastRemoteObject. The server uses the RMISecurityManager to protect its resources while engaging in remote communication.
/* SumRemoteImpl.java */
import java.rmi.*; import java.rmi.server.*; public class SumRemoteImpl extends UnicastRemoteObject implements SumRemote { SumRemoteImpl() throws RemoteException { super(); } public int sum(int a,int b) throws RemoteException { return a + b; } }
The server must bind its name to the registry, the client 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, the RMI security manager is created and installed.
The RMI system provides an RMI compiler (rmic) that takes your generated interface class and procedures stub code on its self. rmic -vcompat SumRemoteImpl
The RMI applications need install to Registry. And the Registry 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 <new port> > start rmiregistry
Step 7:
Scalability: Objects can be distributed across multiple computers while their location remain transparent to clients. 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
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 programmatically writing 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 declaratively By an Application Server
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
Our first step is to try running the server. You'll find a bin directory inside the main JBoss directory which contains various scripts. Execute the run script run.bat. The last message (obviously with different values for the time and start-up speed) should look like the following.
12:31:23,996 INFO [Server] JBoss (MX MicroKernel) [4.0.2 (build: CVSTag=JBoss_4_0_2 date=200504191712)] Started in 47s:608ms
You can verify that the server is running by going the JBoss web server, which is running on port 8080. 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.bat from the bin directory.
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 in the 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 click Next>. Finally, we need to select a domain, host, and port for our application server, and then click on the Finish button.