Simplifying EJB Development with EJB3.0

xiaoxiao2021-03-06  40

Simplifying EJB Development with EJB3.0

Simplify EJB Development October 2004 using EJB3.O

Discussion discussion

Enterprise JavaBeans (EJB) was introduced for building distributed components. When it arrived it came with a promise to solve all issues and complexities of CORBA. EJB being the heart of J2EE went through several major revisions and got fattened with many features. Early on, most of the developers fell in love with EJB and used EJB in their application even it did not make any sense. "Blame it on EJB" has been the attitude for many developers when their projects did not scale well and they used EJB.

We introduce EJB to construct distributed components. It is born to solve all CORBA problems and complexity. After several important version updates and increase many features, EJB has become the core of J2EE. In the early days, many developers were addicted to EJB or even in their works in their work without any meaning. And when they found that the project used did not change, it condemned EJB into a trend.

Development of EJB was never easier and it became more complex with every release of EJB specification. EJB has been compared with an elephant due to its complexity and heavy weight nature. Many developers feel EJB is like an extra layer of sugar syrup on a doughnut. In an age where low carb and Atkins diet is craze, the EJB expert committee has no option but to produce a low carb incarnation of EJB thus simplifying the development of EJB. EJB 3.0 expert committee released a sample picture of the lightweight model during JavaOne 2004 When IT AnnsuNced Release of First Public Draft of EJB 3.0 Specification.D.cides WHEN IT PUBLIC DRAFT OF EJB 3.0 Specification.

Developing EJB has never become more complicated even in the early EJB version specification. EJB is compared to an elephant due to its complexity and heavyweight characteristics. Studying more developers feel EJB like a deep layer of sweet syrup on a donut. Today, now Low Carb and Atkins Diet, the EJB Expert Committee also did not choose the room to release the EJB specification reflected in Low Carb to simplify EJB development. EJB3.0 Expert Committee released an example diagram of a lightweight model called EJB3.0, the first public specification of EJB3.0 at the 2004 Javaone Conference.

At the first glimpse the new model for EJB looks pretty and in this article we will discuss how EJB 3.0 is trying to dress up in a smaller size pretty outfit to make it appealing for developers. In a follow up article we will discuss how EJB 3.0 IS Simplifying The Persistence Model. The first look of the new model of EJB feels very good. In this article we will discuss how EJB3.0 uses a smaller and exquisite assembly to attract developers. In the next article we will discuss how EJB3.0 simplifies the persistence model.

Cleaning Up The Dirty Laundry Squiring Defect

Let Us Look at the ComplexIns in The Current EJB Model Before We Dove Discussing The Details What EJB 3.0 is Bringing on to the table.

Before we start discussing the new features brought about EJB3.0, let's first analyze the complexity of the EJB model now.

...............

The current EJB model needs to build many component interfaces and implement many unnecessary rollback methods.

The Component Interfaces Require IMPLEMENTATION OF EJBOBJECT OR EJBLOCALOBJECT AND HANDLING OF MANY Unnecessary Exceptions Is Required.

The component interface needs to implement EJBOBJECT or EJBLOCALOBJECT and handles many unnecessary exceptions.

The EJB Deployment Descriptor is Complex and Error Prone.

EJB deployment describes the complex and easy error.

The container managed persistence being based on the EJB model is again overly complex to develop and manage. There are several basic features missing such as a standard way to define a primary key using database sequence and EJBQL is very limited.

The development and management of container persistence management based on EJB model is too complicated. Many basic features are not considered, such as using database sequences and EJBQL definitions of a primary key standard method is very limited.

.

The EJB component is unlike object-oriented, such as there are too many restrictions when using inheritance and polymorphism.

One Major Drawback of EJBS IS That You Cannot Test EJB MODULES OUTSIDE An EJB Container and Debugging An EJB Inside The Container IS A Nightmare for Developers.

A main EJB disadvantage is that you cannot get an EJB model with an EJB container and cannot debug an EJB in the container, which is undoubtedly a terrible thing for developers. If you use ejb you know the completion of ejb. It is the you have to know minute details of jior application.

If you use EJB you need to be familiar with the complex process of calling and finding EJB. Obviously, you just want to use EJB in the program but you must know the Detailed details of JNDI.

Simplifying developers View Simplified developers

If you developed an EJB with the latest specification you realize how difficult is develop a simple EJB like HelloWorld EJB. You need at least two interfaces, a bean class and a deployment descriptor. Most of the developers wondered why do I need all these. IDEs like Oracle JDeveloper, Eclipse and XDoclet made the life simple for developers for doing these mundane tasks however still it was the responsibility of developer to compile these classes and package the deployment descriptor before the EJB can be deployed in your container of choice.

If you use the existing version of EJB you will know how difficult it is to develop a simple EJB program such as HelloWorld. You need at least two interfaces, a bean class, and a deployment description file. Most developers want to know why I need all these. Ides (Development Environment Tools) Simply simplified developers' doing these ordinary work development cycles, but before the EJB is deployed, compiling classes and packaging deployments are still developing before you deployed to the selected container. Personnel work.

EJB 3.0 IS Trying to Address the complexities by:

EJB3.0 attempts to simplify complexity from the following aspects:

Removement descriptors and sal be generated by the Container Using Metadata Annotations

You don't have to define the interface and deployment description file, which can be generated by the container using Metadata Annotations.

Use Regular Java Classes as EJBS AND Regular Business Interfaces for EJBS

Use common Java classes as EJB classes and common EJB service interfaces.

Metadata annotations metadata description

EJB 3.0 depends heavily on metadata annotations. Metadata annotation being standardized under JSR 175 and will be part of J2SE 5.0. Annotation is a kind of attribute oriented programming and similar to XDoclet. However unlike XDoclet that requires pre-compilation, annotations are compiled into the classes (depending upon what the @Retention is set to) by the Java compiler at compile-time. in developer's point of view, annotations are modifiers like public and can be used in classes, fields, methods, parameters, local variables, constructors, enumerations and packages.You can use annotations in your Java code by specifying attributes that can be used for either generating code, documenting code or providing special services such as enhanced business-level security or special business logic during runtime. The goal of J2EE 1.5 ( 5.0) Is to Simplify Development Using Annotations and Hence It Will Come Up with ITS Set of Annotations. Annotations Are Marked with @ as Follows: EJB3.0 Very Relying on Metada TA annotations. Metadata Annotations has become a JSR 175 standard and will be part of J2SE 5.0. ANNOTATIONS is an object that is becoming an object, which is very similar to xDoclet. However, it is not necessary to pre-compile like XDoclet, and Annotations compiles when compiled by the Java compiler. (Depending on the start time of @Retrion). In the viewpoint of developers, Annotations is like a public and can be used as a class, domain, method, parameters, local variables, construction, enumeration, and packages. You can use ANNOTATIONS to generate code, automatically write document code, or provide special services such as enhancing business level security or special business logic during operation. J2EE1.5 (5.0) The goal is to simplify developers using Annotations so there is a set of Annotations templates. Annotations uses @ to mark as follows:

@Author ("Debu Panda")

@Bean

Public class mysessionBean

The goal of EJB 3.0 is to simplify development and hence use metadata annotations to generate several artifacts like interfaces and use annotations instead of deployment descriptors.EJB3.0 To simplify the development using the Metadata Annotations so as to produce as many interfaces human factors and use Annotations To replace the deployment description file.

Using Pojos and Pojis Using Pojos and Pojis

In canonical terms, JavaBeans and interfaces often referred as Plain Old Java Objects (POJOs) and Plain Old Java Interfaces (POJIs) respectively. The EJB class and interface will now resemble POJO and POJI respectively. The requirement of the unnecessary artifacts like home interface is Being removed.

In specification conditions, JavaBeans and interfaces often relate to simple Java objects (Pojos) and simple Java interfaces (Pojis). These unnecessary people such as HOME interface have been removed.

The developer has to either implement one of the EJB interfaces (SessionBean, EntityBean or MessageDrivenBean) in javax.ejb package or use annotation in the bean implementation class. You can either use Stateless, Stateful, MessageDriven or Entity to annotate your bean class. For Example, if you define a stateless ejb as helloworld you define the EJB As Follows:

Developers must implement an EJB interface (session bean, entity bean or message driver bean) in the Javax.ejb package, or select ANNOTATION in the implementation class of the bean. You can use a stateless, status, message driver, or entity to comment a bean class. For example, if you define a stateless EJB as a HelloWorld, you can define EJBs as follows:

@Remote

@StateLess Public Class HelloWorldbean {

Public String Sayhello (String S)

{System.out.println ("Hello:" s;}

}

The interfaces for EJBs either remote or local do not have to implement EJBObject or EJBLocalObject. You have to either supply the business interface for your EJB and implement that interface in your bean class or have it generated during the deployment. Interfaces are optional for Entity beans however required for SessionBean and MessageDrivenDriven. If you do not implement an interface for your session bean, a bean interface will be generated for you. The type of generated interface either local or remote is dependent on the annotation you used in the bean class. If you look at the above code example, it is very clear that @Remote is used to generate a remote interface for the HelloWorld bean. If needed, you may have both remote and local interfaces for your EJB.EJB interface, whether remote or local Don't have to achieve EJBOBJECT and EJBLOCALOBJECT. You either serve the business interface for EJB and implement interfaces in the bean class, or you need to generate these interfaces when deploying. Although the interface of the session bean and the message-driven bean is required, the interface of the entity bean is optional. If you don't implement an interface for your session bean, it will automatically generate one for you. The generated interface is local or remote dependent on Annotations in the Bean class. If you take a closer look at the code paradigm above, @ transote is obviously used to generate a remote interface for your HelloWorld. If you need, you can generate remote and local interfaces in your EJB.

From the Above Example, IT IS VERY CLEAR That The Developer Do Not Have To a Lot of Mundane Tasks Such As Defining Interfaces and Implementing Callback Methods.

In the example above, it is clear that developers do not have to do these ordinary work such as definition interfaces and rollback methods.

The name of the generated interface is derived from the name of the bean implementation class. Generated interfaces are well and good for developers. However, I do not see any benefit of generating interfaces as most IDE like Oracle JDeveloper generates these on fly.

The name of the generated interface is derived from the name of the bean implementation class. The generated interface is very useful for developers. But I didn't see any of these IDEs such as Oracle's JDeveloper immediately implemented this generated interface function.

This is not clear from the draft what is client side requirement for EJB lookup and it is not clear, how do we get hold of these interfaces those are required to invoke this EJB I will recommend against using a generated interface for few reasons:? Specification There is no clear demand for the client when EJB lookup, nor is it clear how we keep these EJBs need to be called. Based on the following cases I will not recommend the generated interface:

The name of generated interface will be deived from the bean name

The name of the generated interface is derived from the name of the bean

You May Not Be Willing To Expose Some Methods In The EJB in The interface and the generated interface ..

If you don't want to expose some methods in EJB and generate an interface to expose all methods by default.

You need to invoke the ejb.

You need to use the interface to call EJB in the client.

Remiving Need for Callback Method to remove the needs of the rollback method.

EJB 2.1 and prior releases required implementation of several lifecycle methods such as ejbPassivate, ejbActivate, ejbLoad, ejbStore, etc. for every EJB even if you do not need those. For example, ejbPassivate is not required for a stateless session bean still you require to implement that method in your bean class. As EJB 3.0 now resembles regular Java classes implementation of these lifecycle methods have been made optional. If you implement any callback method in the EJB then container will invoke that method.

EJB2.1 and earlier versions require many ways to achieve some life cycles that you don't need for each EJB, such as EJBPassivate, EJBActivate, EjbLoad, EJBStore, and more. For example, there is no need for EJBPASSIVATE in the stateless session bean but you still have to implement it. The method of similar Java classes in EJB3.0 now achieves these life cycles are made into optional. These methods are called if you implement any rollup container in EJB.

The only exception is the ejbRemove method in Stateful Session bean where you can use Remove annotation to annotate a Stateful session bean business method. If you use of this annotation it will hint the container to remove the Stateful session bean instance after the completion (normal or abnormal) of the annotated method. For example, you can specify the following to remove a Stateful session bean instance after checkOut method is executed. the only exception is a stateful session ejbRemove method business method of the bean when you can use annotations Remove the Is the state session bean. If you use this annotations it will remove the status session bean instance after completing the AnnotationS method (regardless of normal or abnormal) prompts. For example, you can specify the following way to remove a status session bean instance after the Checkout method is executed.

@Stateful public class cart {

...

...

@Remove public void checkout () {

...

}

}

Annotations vs. Deployment Descriptors

Comparison of annotations and deployment descriptions

As we discussed earlier deployment descriptors will no longer be required for EJBs and annotations will be used instead. The default values ​​for each attribute in the deployment descriptor will be chosen and developers do not have to specify these attributes unless they want a value other than the default value. These can be specified using annotations in the bean class itself. The EJB 3.0 specification is defining a set of metadata annotations for use by developers such as bean type, type of interfaces, resource references, transaction attributes, security, etc. for Example, IF WE Want To Resource Reference for a Particular EJB THEN WE WILL Define As Follows:

In the previous item, we will no longer need deployment description in EJB and by Annotations. The properties of each deployment will be selected for a default value, while the developers do not have to specify values ​​for these attributes until they want to change the default values ​​of these properties. These can also be used for the ANNOTATIONS used for the class of Beans itself. EJB3.0 Specifications For developers using bean type, interface type, resource reference, transaction attribute, security, etc. to define a set of Metadata Annotations. For example, if we can use the resource reference for a special EJB definition:

@Resource (name = "jdbc / OracleDS", resourceType = "javax.sql.DataSource") J2EE vendors such as Oracle, BEA, IBM will add annotations for attributes in their vendor specific deployment descriptors and developers will use these to avoid using deployment descriptors. This looks very attractive for developers as the nasty XML descriptors they hated most gets out of their way but this introduces few problems and we need to be careful before embracing annotations.

J2EE's providers such as Oracle, BEA, IBM will increase attribute Annotations In their specified deployment description, developers will be able to use these Annotations to avoid using deployment descriptions. This looks very attractive to developers, especially the XML description is a developer who already disgusted, and they have long hate and want to leave the old description of the old, but there are still some problems that make us officially use Annotations. Need to be cautious.

This is the ejbs.

It violates the goal of our light application, because if an EJB uses a provider specified deployment description, it must change multiple times when recompilating or packing EJB.

The deployment descriptors provided a holistic view of EJB modules to the assembler / deployer (ejb-jar) without having to look at individual the EJBs and they tweaked these as per the deployment requirement and if descriptors are not available or not generated until the end of Deployment this could be a nightmare for them.

Deployment Description Provides a global view to the EJB template for assembly and deployment, you don't have to consider separate EJBs, and they are screwed together for each deployment requirement, and the description is invalid or not automatically generated before deployment. This is a terrible thing for deployers.

Deployment descriptors were used by the tools to identify the EJBs in an EJB module and were very useful when you try to migrate from one container to another.EJB 3.0 spec also proposes a way to override the annotations in the deployment descriptors. However details of overriding Annotations is missing in the specification.

Deployment Description is used in the EJB template to define EJBs when you try to integrate one with another container is very useful. EJB3.0 specifications also advocate how to use overload ANNOTATIONS in the deployment description. However, there is no detail to overload annotations in the specification. There Is No Doubt That Getting Rid of Deployment Descriptor Will Make The Life Easier for New Developers But this May Become a Management Nightmare if not buy Carefully.

Undoubtedly getting rid of deployment, which will make new developers easier, but if it is improper use, this will also cause terrible issues.

Simplifying Container Managed Persistence Simplified Persistence Container Management

CMP entity bean is getting a major overhaul in EJB 3.0 to make it compelling for developers. Persistence frameworks like OracleAS TopLink, open source Hibernate have become darling of developing persistence framework for J2EE applications that has disliked Entity beans for its complexity and heavyweight in nature. EJB 3.0 is adopting a lightweight persistence model like TopLink and Hibernate to simplify the container managed persistence and this sounds compelling for developers. Let us do a quick look on the plans for entity beans and we will discuss about the details of the persistence improvement in another Article.

The CMP entity bean will become EJB3.0 to enable developers to enforce a major inspection method. Persistence frames such as Oracleas Toplink, open source Hibernate have become unlike the complex and heavyweight development J2EE persistent framework applications like entity Beans. EJB3.0 adopts Toplink and Hibernate lightweight persistent mode to simplify container persistence management, which sounds more happy. Let us browse the entities of the entity bean, we will discuss details of persistent improvements in another article.

Entity beans are being reincarnated as POJOs and there will be no component interfaces required for entity beans. Entity beans will now look as pure objects as this will also support inheritance and polymorphism.

The entity bean has been changed to the POJOS, and the entity bean will not need the component interface. Entity Beans now look into a pure object that supports inheritance and polymorphism.

Following is the source code for an entity bean:

The following is about the entity bean source code

@ENTINTITY PUBLIC CLASS EMPLOYEE {

PRIVATE Long Empno;

PRIVATE STRING Empname;

Private address address;

PRIVATE HASHMAP Projects = New HashMap (); Private Double Saliff;

@ID (generate = sequence) public long positionEmpno () {

RETURN EMPNO;

}

Protected void setempno (long Empno) {

THIS.EMPNO = Empno;

}

Public string geTempname () {

Return Empname;

}

Public void setempname (String Empname) {

this.empname = Empname;

}

@Dependent public address getaddress () {

Return Address;

}

Public void setaddress (address address) {

THIS.ADDRESS = Address;

}

Public set getProjects () {

Return Projects;

}

Public void setProjects (set projects) {

THIS.PROJECTS = Projects;

}

Public Double getSalary () {

Return Salary;

}

Public void setsalary (double salary) {

THIS.SALARY = SALARY;

}

....

}

IF you look at the code, you will find what the bean class is a concrete class and not an abstract class the current entity bean.

If you look at these code carefully, you can find that the Bean class in the current entity bean is a specific class and no longer an abstract class.

There are several improvements are being made for the query capabilities in EJB QL and support of SQL queries in the entity beans. A new EntityManager API similar to Hibernate and a simplified version of TopLink 'Session API is being proposed to manipulation of entity beans ie creation , REMOVAL, FINDING OF Entity Beans.

EJB QL and entity beans have made multiple improvements. A new entity management of Hibernate management API and Simplified version of Toplink's session API is recommended to handle the operation of entity beans, that is, the creation, release, and lookup of entity beans.

We will Closely Examine The Details of The Proposed CMP Entity Beans in a FOLLOWUP ARTICLE.

We will further make more verification on the details of the suggestion of the CMP entity bean in an in-depth article.

Simplifying Client View for EJBS Simplifies EJB Clients

Using EJBS I.E. To Lookup and Invoke Is Very Complex Even IF EJB IS COLLOCATED IN THE Application. J2EE 1.4 And EJB 3.0 Spec is Working ON To Simplify The Client's View of EJBS.

Even if EJB is serialized, it is also very complicated using EJB to find and call. J2EE 1.4 and EJB 3.0 Specifications are committed to simplifying the EJB client. If you want to use an EJB presently you have to define the ejb-ref or ejb-local-ref in the deployment descriptor, lookup the EJB and then invoke. If we want to invoke our HelloWorld EJB, following is the simplest way you can Invoke an ejb with the current importation.

If you want to use EJB you must define an EJB reference or EJB local reference in the deployment description, find the EJB and then call it. If we want to call HelloWorld's EJB, you can call EJB to use existing implementations as follows.

First Define The EJB REFERENCES in The Deployment Descriptor as Follows:

Find the definition of EJB indication in the deployment description

HelloWorldejb

Session

Hello.HelloWorldHome

Hello.HelloWorld

. T....................

Then look for EJBs as follows. You need to express the exception when ejb lookup and establish a bean instance.

Try

{

Context context = new initialContext ();

HelloWorldHome HelloHome =

(HelloWorld) PortableRemoteObject.narrow (Context.lookup

("Java: Comp / Env / EJB / HelloWorldejb"), HelloWorldHome.Class;

HelloWorld Hello = HelloHome.create ();

....

}

Catch (RemoteException E)

{

System.err.println ("System / Communication Error:" E.GetMessage ());

}

Catch (Namingexception E)

{

System.err.println ("Communication Error:" E.getMessage ());

}

CATCH (CREATEXCEPTION E)

{

System.err.Println ("Error Creating EJB Instance:" E.GetMessage ());

}

AS An Alternative to Environment Variables, EJB 3.0 Proposes A Way To Use Setter InjBS.

As a change in environment variables, EJB3.0 recommends finding and calling EJBs using methods of placing inJection.

FOLLOWING IS WAY OUR HelloWorldejb Can Be Looked Up Another EJB BY Using SETTER INJECTION. Below we use the method of placing the inJection in another EJB lookup HelloWorldejb.

@INJECT Private Void SetSessionContext (sessionContext CTX)

{

this.ctx = CTX

}

...

MyHello = (HelloWorld) CTX.lookup ("Java: Comp / ENV / EJB / HelloWorldejb");

If you look at the code above carefully the setSessionContext method is annotated with @Inject to specify that dependency injection be used for this method. The injected methods will be invoked by the container to set the EJBContext before any business method be called on that EJB.

If you look carefully to the code above, its dependent INJECTION uses @ITSet to specify the Annotations for the sessionContext method. The INJECTION method will be able to call the EJBCONText before the business method is called before being called by the EJB.

Another Direct Example To Inject The HelloWorld Session Bean Would Be To Simply Have @EJB Public HelloWorld Myhello and this Would Cause MyHello To BE INJECTED WITH ANSTANCE OF TO BEELLOWORLD BEAN.

Another direct example of another injected HelloWorld session bean can be simplified to use

@Ejb public helloworld myhello, which will make Myhello by the HelloWorld Bean instance injected.

YOU CAN Use Dependency Injection To Lookup Any Type of Environment And Resource References Such As Datasource, JMS, Mail, Web Service, etc.

You can use the dependent INJection to query any type of environment and resource references such as DataSource, JMS, Mail, Web Service, and more.

Testability USAbility Outside Container External Test Feasibility

A major concern for the current EJB developers is not only development of EJBs is complex but also testing is a nightmare. An EJB container is required is to develop and test the EJBs and the developers have to be familiar with the final deployment platform to perform testing . This may not be a major issue for many enterprise developers but is an issue for ISVs who work on supporting multiple vendors and they have to maintain multiple environments to do the testing of their EJBs. The EJB 3.0 spec promises to provide the ability to test Outside The Container But this Is Currently Missing In The Draft Specification. A now I'm not only the complexity of EJB development, but also the terrible of the test. Development and test EJBs must require an EJB container and developers must be familiar with the final deployment platform to perform tests. This is a big problem for many business developers who are developing only a major platform, but is a big problem for the support of multiple developer platforms and to test EJB ISVs in maintenance. The EJB3.0 specification promise will provide the function of the container test, but this is lost in this specification.

Conclusion conclusion

Although there are a lot of missing information about packaging, assembly and gory details of APIs, the proposals made in EJB 3.0 drafts looks very promising for enterprise java developers. These will certainly help remove the complexities from developers view by passing those to the container vendors . It is to be seen how the Container Vendors Implement these and make ejb

3.0 a

Compelling Option to Develop Enterprise Applications.

Although there are still many details about packaging, assembly and important API details, there is no mention of this norm, but many recommendations make EJB3.0 specifications to make enterprise Java developers look forward to their future. Through these undoubtedly, it will help reduce development complex from developers to service providers. Here's how to see how service providers are implemented, and make EJB3.0 a leader of enterprise application.

Author Bio Author

Debu Panda is a principal product manager of Oracle Application Server development team, where he focuses his efforts on the EJB container and Transaction Manager. He has more than 13 years of experience in the IT industry and has published articles in several magazines and has presented at Many Technology Conferences.debu Panda is the main manager of the Oracle Application Services Development Team, and his achievements have been achieved in EJB containers and transaction management. He has 13 years of experience in IT industry and has published a lot of works in a number of magazines and technology publishers.

Translation problem:

Since some English nouns have not dared to translate, the original word in English is in the text.

Cleaning Up The Dirty Laundry Squiring Defect

Low Carb

Atkins Diet

Metadata annotations metadata description

Simplifying Client View for EJBS Simplifies EJB Clients

Dependency Injection Dependency INJECTION

Setter Injection Ontal Injection

ISV Independent Software Vendor Independent Software Supplier

转载请注明原文地址:https://www.9cbs.com/read-74448.html

New Post(0)