Disclosure of Invention
The invention provides a method and a system for realizing J2EE application, which are used for solving the problem that the EJB component in a J2EE application system is complex in design due to the use of a large number of EJB components in the prior art.
The invention provides the following technical scheme:
a method of implementing a J2EE application, comprising the steps of:
the EJB client module which receives the client service request sends service request information to a service request proxy module, wherein the service request proxy module consists of an EJB component;
the service request agent module acquires service object configuration information and acquires a service object instance according to the configuration information;
and the service request agent module calls the method of the service object instance to process the service according to the service request information and returns the processing result to the EJB client module.
The step of obtaining the configuration information of the business object comprises the following steps:
the service request agent module analyzes the service request information of the EJB client to obtain a service object type path identifier, a method identifier and parameter information;
the service object information configuration module loads corresponding service object configuration information according to the class path identifier and the method identifier of the service object specified by the service request information, and the information is returned to the service request agent module after being stored in an XML file format or a database format.
The service request information comprises service object type path identification, method identification and parameter information.
The service object configuration information comprises a service object class path name, a method name and transaction attribute information.
And the service request agent module acquires corresponding service object instances from a service object pooling module containing a plurality of service object instances according to the acquired service object configuration information.
The method for obtaining the business object instance also comprises the steps that the business object pooling module judges whether a corresponding business object instance exists, and if the business object instance exists, the business object instance is directly returned to the business request agent module; otherwise, creating a service object instance and transmitting the service object instance to the service request proxy module.
The business object pooling module automatically loads all business object classes during system initialization according to the business object configuration information provided by the business object information configuration module, then initializes the business objects according to the definition of the buffer pool in the business object pooling module, and instantiates the business objects through a Java reflection mechanism.
The business object pooling module stores business object instances according to a buffer pool specification, wherein the buffer pool specification comprises the initialized number and the maximum buffer number of the business objects.
And the service request agent module positions corresponding method objects and transaction attribute information in the service object pooling module according to the obtained service object instances.
Defining a method transaction processing mechanism of a service object as a container management transaction, wherein all service method calls executed by a service request proxy module are guaranteed by the transaction of the container; or defining the method transaction processing mechanism of the service object as a Bean self-management transaction, starting the transaction before calling the method by the service request agent module according to the transaction declaration in the configuration information, and submitting or rolling back the transaction after the method is executed.
A system for implementing a J2EE application, comprising: the Web container is used for responding, scheduling and analyzing the service request, and comprises an EJB client module used for calling EJB service logic corresponding to the service request; the EJB container is used for specific application service logic and background data persistence;
wherein the EJB container includes:
the business object information configuration module is used for configuring the business object and storing the configuration information of the business object;
the business object pooling module is used for providing business object instances and is responsible for data persistence of the business objects;
and the service request proxy module is formed by an EJB component and is used for receiving service request information sent by the EJB client module, acquiring object configuration information from the service object information configuration module, acquiring a service object instance from the service object pooling module according to the configuration information, calling a method of the service object instance to perform service processing and returning a processing result to the EJB client module.
And the service information configuration module loads corresponding service request object configuration information according to the class path identifier and the method identifier specified by the service request information, and the information is stored in an XML file format or a database form.
And the service request agent module positions corresponding method objects and transaction attribute information in the service object pooling module according to the obtained service object instances.
The business object pooling module automatically loads all business object classes during system initialization according to the business object configuration information provided by the business object information configuration module, then initializes the business objects according to the definition of the buffer pool in the business object pooling module, and instantiates the business objects through a Java reflection mechanism.
The business object pooling module stores business object instances according to a buffer pool specification, wherein the buffer pool specification comprises the initialized number and the maximum buffer number of the business objects.
The invention has the following beneficial effects:
1. the service components adopt common POJO (Plain Old Java Objects) type components, so that the quantity of EJB components is reduced, the workload of development, deployment and maintenance of the J2EE system is greatly simplified, and the working efficiency of the system is improved;
2. by adopting a configuration mapping mechanism of a service object, the realization of a presentation layer and a service logic layer in a J2EE application system can be completely separated only by setting a configuration identifier when a client calls a service logic, so that the coupling of the system is reduced;
3. the architecture provides a flexible and customized transaction processing mechanism, and transaction management in the service development process is simplified;
4. the service object adopts a buffer mechanism, so that the service processing capacity of the system is improved.
Detailed Description
In order to solve the problem that the design of a J2EE application system is complex because a large number of EJB components are used for realizing service logics of different services, the invention uses one EJB component as a proxy to forward information by reducing the number of the EJB components, and adopts common POJO classes to realize the service logics.
As shown in fig. 2, the J2EE application system in this embodiment includes a Web container and an EJB container, where the Web container is used for response, scheduling, and parsing processing of a service request, and includes a JSP/Servlet component module used for implementing representing service logic and submitting a service request object; the EJB client module is used for receiving the service request object information and sending a service request; the EJB container is used for specific application service logic and background data persistence and comprises a service object information configuration module, a service object pooling module and a service request agent module, wherein,
and the service object information configuration module adopts a common JavaBeans component and is used for configuring the service object, storing the configuration information of the service object, and maintaining information such as the class path name, the method name, the transaction declaration of the method and the like of the service object.
And the service object pooling module adopts a common JavaBeans component and is used for providing a service object instance for the service request agent module and is responsible for data persistence of the service object, so that frequent creation and destruction of the service object instance are reduced.
And the service request proxy module adopts a standard EJB component and is used for receiving the service request sent by the EJB client module, acquiring the configuration information from the service object information configuration module, acquiring the corresponding service object example from the service object pooling module according to the configuration information, calling the method of the service object example to perform service processing and returning the processing result to the EJB client module.
As shown in fig. 3, the service request processing procedure in this embodiment is as follows:
and step 300, the application client sends a service request to the JSP/Servlet component module.
And 301, after receiving the service request of the application client, the JSP/Servlet component module analyzes the service request and submits the service request to the EJB client in a service request object mode.
The creation code of the service request object and the service request initiated by the EJB client can be realized by the following codes:
v/creation of EJB client, the following string parameter being the invoked Business configuration object
EJBClient client=new EJBClient
(″com.huawei.msp.bean.CommonFacadeBean″);
V/define service request object
public class RequestValue implements CompositeValue
{
v/Call Bean object interface configuration name
private String requestCode;
// parameter List
private ArrayList paramList=new ArrayList(2);
/**
Default constructor
*/
public RequestValue()
{
requestCode=Constant.CONST_EMPTY;
}
/**
Obtaining calling Bean object interface configuration name
*@return String
*/
public String getRequestCode()
{
return this.requestCode;
}
/**
Input parameter list of get request interface
*@return ArrayList
*/
public ArrayList getParamList()
{
return this.paramList;
}
/**
Setting and calling Bean object interface configuration name
*@param requestCode String
*/
public void setRequestCode(String requestCode)
{
this.requestCode=requestCode;
}
/**
Setting parameters to request object
*@param param Object
*/
public void addParam(Object param)
{
this.paramList.add(param);
}
}
V/construct service request object and assign value
RequestValue request=new RequestValue();
request.setRequestCode(″TestBean.query″);
request.addParam(″2001021823231″);
V/initiate service request
Client.callService(request);
Step 302, after the EJB client analyzes the service request object information, the EJB client sends the service request to the service request proxy module.
The EJB client analyzes the message of the service request object and remotely calls a service request proxy interface in an RMI-IIOP (Remote method invocation Internet Inter-ORB Protocol) Protocol mode; the method comprises the steps of firstly providing JNDI (Java Naming and Directory Interface) service through an application server to obtain a Home Interface of a service request proxy, then establishing a remote operation Interface of the service request proxy through the Home Interface, and finally calling the service request proxy Interface.
When the EJB client calls the service logic, the service object is not directly called, but the service logic is called by proxy distribution of the service request proxy module. Therefore, when the EJB client sends a service request, a strict service request information format needs to be defined, including the class path identifier, the method identifier, and the parameter information of the service object. An example of code is as follows:
// get Facade (local) Home object instance
Object object=Context.lookup(className,jndiName);
V/through mapping mode, creating FacadeBean object remote instance by Home object
DelegateEJB delegate=(DelegateEJB)BeanUtil.invokeMethod(object,″create″,null);
// calling EJB component's service agent interface
Delegate.callService(request);
Step 303, after receiving the remote invocation request, the service request proxy module sends the service request information to the service object information configuration module to obtain the configuration information of the service request object.
The service request agent module analyzes the service request information of the client to obtain the class path identification, the method identification and the parameter information of the service object.
The service object information configuration module loads the configuration information of the corresponding service object according to the class path identifier 'TestBean' and the method identifier 'query' specified by the service request information, and the information is returned to the service request agent module after being stored in an XML file format or a database form. The service object configuration information includes a class path name, a method name and a transaction attribute of the service object. Each configured business object can be associated with a unique identifier, and business declaration information of the method and the method of the business object is maintained under the business object. The code example of the configuration information of the queryTestInfo of the query method for loading the group user management service class is as follows:
<?xml version=″1.0″encoding=″UTF-8″?>
<config>
<module id=″common″>
<bean id=″TestBean″name=″com.huawei.test.bean.TestBean″>
<method id=″query″name=″queryTestInfo″transaction=″true″/>
</bean>
</module>
</config>
in an example, < Bean id ═ TestBean "name ═ com.huawei.test.bean.testbean" > identifies the configuration of the business Bean object, "TestBean" is an object configuration identifier, and "com.huawei.test.bean.testbean" is a class path name that identifies the corresponding class; "query" name "transaction" false "/> is method configuration information," query "is a method configuration identifier, and" queryTestInfo "is a method name. In the request object, the actual request information is replaced by the identification call sign, so that the 'testbean.query' identifies the methods of the service object com.
Step 304, the service request agent module obtains the service object instance in the service object pooling module.
And the service request agent module acquires the corresponding service object instance from the service object pooling module containing a plurality of service object instances according to the class path name and the method name of the service object.
In order to improve the service object request efficiency, the service object is added with a function of cache pooling. According to the service object configuration information provided by the service object information configuration module, the service object pooling module automatically loads all service object classes through a class loader (ClassLoader) in the Java virtual machine when the system is initialized, then initializes the service object according to the definition of the buffer pool in the service object pooling module, and instantiates the service object through a reflection mechanism of Java. The definition of the buffer pool comprises the implementation principle and architecture of the buffer pool and configuration information of relevant buffer pool specifications, wherein the main specifications of the buffer pool comprise the initialized number and the maximum buffer number of the business objects. When calling the client, the service request agent module can quickly locate the corresponding service object instance in the service object pooling module according to the configuration information provided by the information configuration module.
If the business object instance already exists in the business object pooling module and the state is idle, directly returning the reference of the object instance to the business request agent module for calling the business method, and simultaneously setting the state of the object to be busy; if the service object instance state is busy, determining the reference mode of the instance according to the service quality requirement level of the service request, and if the quality requirement is low, waiting for the reference when the service object state is idle; if the quality requirement level is higher, or the service object instance does not exist in the service object pooling module, an instance of the service request object is created in a Java reflection manner, stored in a buffer pool, and the instance reference is transmitted to the service request proxy module, and the state is also set to be busy (implementation of the instance structure and acquisition of the method object, please refer to a Java application programming Interface-API (application programming Interface-API) provided by Sun, which is not described in detail herein).
And the service object instance is not deleted along with the service end of the client, after the calling of the service object instance is finished, the service object reference is returned and stored in the service object pooling module, and meanwhile, the state of the service object instance is set to be idle. The buffer mechanism of the business object reduces the frequent creation and destruction of the business object and realizes the persistent management of the business object.
Step 305, the service request agent module locates the method object of the service object and the transaction attribute of the method object according to the obtained service object instance.
Step 306, the service request proxy module initiates a service call request to the service object pooling module.
After receiving the service calling request, the service object pooling module acquires relevant service information from the database, and the service request proxy module calls a method of the service object instance according to the acquired transaction attribute information of the service object instance, the method object and the method object; an example of code is as follows:
assume that the TestBean is implemented as follows:
public class TestBean implements BsnBean
{
public List queryTestInfo(String testid)throws PException
{
List list=null;
// service logic implementation
return list;
}
}
Then according to the business object and method returned from the above steps, the calling mode actually through the reflection mechanism is as follows:
// Start-up Process
UserTransaction userTransaction=null;
If(transaction)
{
userTransaction=getTransaction(methods);
userTransaction.begin();
}
// method invocation
Object result=method.(testBean,new String[]{“2001021823231”});
// committing a transaction
If(userTransaction!=null)
{
userTransaction.commit();
}
Step 307, the service object pooling module returns the service processing result to the service request agent module.
And step 308, the service request agent module returns the service processing result to the EJB client.
Step 309, the EJB client returns the service processing result to the application client.
In the invention, the transaction management of the business method adopts a self-defined transaction declaration processing mechanism and can be realized by the following two ways:
1. and when the service request agent module is deployed, the declaration attribute of the method is the container transaction support, and all service logic calls executed by the service request agent module can obtain the transaction guarantee of the container. If the service logic is called by the JSP/Servlet technology, the service logic is operated in the Web container of J2EE to obtain the transaction guarantee of the Web container; if the business logic is called by the EJB technology, the business logic runs in the EJB container of J2EE to obtain the transaction guarantee of the EJB container.
2. When the business request agent module is deployed, the declaration attribute of the method is the Bean self-management business, at the moment, the business request agent module can declare according to the business in the configuration information, starts the business before calling the method, and submits or rolls back the business after the method is executed, so that the function of the Bean self-management business is achieved.
If there is no declarative attribute of the method, then the business process does not provide transaction management, reducing consumption of container resources.
In conclusion, the invention simplifies the design of the EJB component, reduces the complexity of development and deployment of the J2EE system, and also reduces the complexity of client application; the coupling between the presentation layer and the service logic layer is reduced by adopting a configuration mapping mechanism of the service object; in addition, the invention provides a user-defined transaction processing mechanism of the method object, simplifies the transaction management in the service development process and improves the working efficiency of the J2EE system.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present invention without departing from the spirit and scope of the invention. Thus, if such modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include such modifications and variations.