Write Enterprise Bean's client (below)

zhaozj2021-02-11  207

Use the handle of the bean:

The handle is another method used to reference Enterprise Beans. The handle is equivalent to a longer pointer of Bean. You can get your handle from the Remote interface. Once you have a handle, you can write it into a file or other persistent memory. Easy later, you can re-get your handle, use it to re-establish an Enterprise Bean.

However, you can only re-establish a reference to the handle of the REMOTE interface. You can't create a bean itself with your handle. If another process deletes a bean, or the system crashes or off, deletes the bean instance, then the application will throw an exception when the application is trying to re-establish the reference to the bean.

When you are not sure if the instance of bean still exists, you don't have to use the handle of the Remote interface. Instead, save the HOME interface of the bean, and then recreate the bean object by calling the CREATE or Finder method when you are used.

After the customer has created an instance of a bean, you can use the getHandle () method to get the handle of the instance. Once you have your handle, you can write it to the file. In the later time, the customer can read this file, convert the read object into handle type. The getejbobject method can then be called on the handle to obtain reference. Finally, the value returned by the getEJBOBJECT method is then converted to the appropriate type.

Use the handle to reference a bean

Import java.io;

Import javax.ejb.handle;

......

Cart Cart;

......

Cart = Home.create (CARTHOLDERNAME, CREDITCARTNUMBER, EXPIRATIONDATE);

/ / Call the GetHander method on the CART object to get its handle.

CartHander = Cart.getHandler ();

// Write Hander to the file.

FileOutputStream f = new fileoutputstream ("carthandle.ser");

ObjectOutputStream O = New ObjectOutputStream (f);

O.WriteObject (myHandle);

O.flush ();

O. close ();

......

// In the next time, you can read the Handle from the file.

FileInputStream Fi = New FileInputStream ("CartHandle.ser");

ObjectInputsteam Oi = New ObjectInputStream (Fi);

// read the object from the file and convert it to the Hander type.

Carthanle = (Handle) oi.readObject ();

Oi.close;

......

// Use Handle to reference the instance of bean

Try {

Object Ref = context.lookup ("cart");

Cart Cart1 = (CART) javax.rmi.portableremoteObject.narrow (Ref, Cart.class);

......

} catch (remoteException E) {

......

}

......

When using the handle of the session bean, the customer will use the javax.ejb.ebhome.remove (Handle Handle) method to delete the handle.

Second, management transactions:

The client can manage its own transaction instead of let Enterprise Beans or containers. When the customer manages its own business, it seems like a session bean manages its own transaction.

When customers manage their own transactions, they need to describe the boundary line of the transaction. That is to say, it must be clearly starting a transaction and termination (submit or rollback) a transaction. Customers use the javax.transaction.usertransaction interface to manage its own transactions. It must first use JNDI to get a reference to the UserTransaction interface. Once there is a UserTranscation context, you can start a transaction with the useertransaction.begin () method (later with the useertranscation.commit () method submitted or the useertransction.rollback () method rolls back this transaction). This customer does the relevant query and update operation.

The following code demonstrates how the customer implements its own transaction. The part of the agreement belonging to the customer is displayed with bold:

Customer management transaction:

Import javax.naming.initialcontext;

Import javax.transcation.usertransaction;

......

Public class clienttransaction {

Public static void main (string [] argv) {

Usertranscation ut = NULL;

InitialContext INITCONTEXT = New InitialContext ();

......

UT = (Usertransaction) INITCONTEXT.LOOKUP ("Java: Comp / Usertranscation);

// Start a transaction

Ut.begin ();

// Do business.

......

Ut.commit (); // or ut.rollback ();

}

}

Third, get information from Enterprise Bean:

The information of Enterprise Bean is a pointing to Metadata references. Customers use the GetMetadata () method of the Home interface of Enterprise Bean to get the bean's metadata information.

GetMetadata () methods are often used by the development environment and build tools because they need to know about Enterprise Beans, such as which links to the beans have been installed. Script customers also need to get the bean's metadata information.

Once the customer remediates the home interface. You can call the getEJBMetadata () method on the HOME interface. The customer can then achieve the following information by calling the method on the EJBMetadata interface.

Use the ejbmetata.getejbhome () method to get the bean's EJBHOME interface.

Use the ejbmetadata.gethomeInterfaecclass () method to get the object of the HOME interface class. Includes its interface, class, domain, and method.

Use the ejbmetadata.getRemoteInterfaceClass () method to get the object of the Remote interface class. Includes all kinds of information.

Use the EJBMETADATA.GETPRIMARYKEYCLASS () method to get the primary key object of the bean.

Whether it is a session bean or an entity bean. The ejbmetadata.ission () method can be used to judge.

Use ejbmetata.isstatelessSession () to determine if a session bean is status or stateless.

Fourth, JNDI support:

The EJB specification defines the API of the positioning home interface. JNDI is implemented in other services (Naming services, LDAP / X.500, Flat Files, Directory Services). The figure below explains different implementation seals. Typically, EJB service providers choose a specific JNDI implementation.

This technique realizes the unrelated relationship between JNDI underlying. Customers can only need to use JNDI API.

5. Map of EJB to CORBA: There are many contacts between EJB and CORBA. There are three points very important:

Implement EJB containers / services with ORB.

Put the original system in the overall capacity of the EJB intermediate layer.

Access to EJB from customers from non-Java components.

The EJB specification is currently only involved in these three aspects.

CORBA is a very suitable and natural platform for achieving the underlying organization of EJB. Corba provides all EJB specifications and CORBA core specifications or related things between CORBA core services.

Division: CORBA core and CORBA naming service

Support transaction: transaction service for CORBA objects

Support security: CORBA security specification, including IIOP-over-SSL

In addition: CORBA allows non-Java components to be combined with applications. These components can be the original system or program, insert them into different customers. The backend system can be easily combined with any OTS and any programming language that can map IDL maps. This requires the container to provide APIs for OTS and IIOP.

The EJB specification allows non-Java customers to access Enterprise Beans, also provides mapping of EJB to CORBA. The purpose of the EJB / CORBA map is:

Support for clients written in any CORBA-supported programming language and Enterprise Beans on CORBA-based EJB services.

Combine the client program in the same transaction processing with the CORBA object or Enterprise Bean.

Support for distributed transactions that are running on CORBA-based EJB servers that involving different agents.

The mapping is based on Java-to-IDL mapping. Its specification includes the following aspects: mapping, naming, mapping, and secure mapping of transactional mapping, mapping of transaction. In the following part, we will explain each mapping. Since the mapping uses new IDL features, these features are introduced in the Object-By-Value specification for OMG. Combine with other languages ​​requires compatibility with CORBA2.3ORB specifications.

Distributed mapping:

Enterprise Bean has two interfaces that can be accessed: Remote interface and home interface. There is such Java / IDL to these interfaces in the IDL specification. The base class defined in the EJB specification is also mapped to the IDL in the same manner.

For example: Let's take a look at the IDL interface below, this is an example of ATM. It is an Enterprise session bean. There is a method transferred between Accounts. You can also throw an abnormality of insufficient funds. Map its HOME interface and Remote interface via Java / IDL. We get the following IDL interface:

Module Transaction {

Module EJB {

VALUETYPE INSUFFICIENTFUNDSEXCEPTION: :: Java :: lang :: exception {};

Exception insufficientfundsex {

:: Transcation :: EJB :: InsufficientFundSexception value;

}

Interface atm: :: javax :: ejb :: ejbobject {

Void Transfer (in string arg [], in string arg1, in float arg2)

Raise (:: Transaction :: EJB :: InsufficientFundsex);

}

Interface atmhome: :: javax.: ejb :: ejbhome {

:: Transaction :: EJB :: ATM CREATE {}

Raise (:: javax :: ejb :: creteex);

}

}

}

Name mapping:

Based on CORBA-based EJB Runtime Environment If you want to allow all CORBA customers to access Enterprise Beans, you must use the CORBA naming service to publish Enterprise Beans's HOME interface. Optional use of CORBA naming services can be used in runtime, or indirect use through JNDI and its standard mapping to CORBA naming services. JNDI Naming uses a string to represent as follows: "Directory1 / Directory2 / ... / Directotyn / ObjectName". Corba's naming service defines the name into a series of name components:

Typedef string istring;

Struct namecomponet {

Istring ID;

Istring Kind;

}

Typedef sequence name;

Each JNDI string name separate from "/" is mapped into one name component. The leftmost component is the first entrance to the CORBA name service name.

JNDI string name is related to some name context. We call this context called the JNDI root context. The JNDI root context is equivalent to the initialization context of the Naming service of CORBA. The name of the CORBA naming service is related to the initialization context of CORBA.

The CORBA application can get the context of the initial CORBA naming service by calling resolv_initial_reference ("NAMSERVICE") on the virtual object of ORB. The CORBA naming service does not specify how to organize the name context so that the concept of the root context does not work. The initialization of the ORB determines the context through resolve_initial_reference ().

For example: a C client is able to locate our ATM session bean's HOME interface. This ATM Bean has been registered with JNDI strings "Transaction / Corbaejb / ATM". First, we get the initial name context.

Oject_ptr obj = orb-> resolve_initial_references ("Nameservice");

NameContext InitialNameContext = NameingContext.Narrow (OBJ);

IF (InitialNameingContext == NULL) {

CERR << "Could Not Initial Naming Context" << ENDL;

Exit (1);

}

We then produced the name of a CORBA naming service and initialized it according to the above.

Name name = new name (1);

Name [0] .id = "ATM";

Name [0] .kind = ""

Now we solve the name of the name of the initial naming service. Assume that we have successfully initiated, we also have contexts on the Enterprise Bean name field. We narrow the range of CORBA objects to the types you need, and confident that this reduction is successful.

Object_ptr obj = initialnamingContext-> resolve (name);

AtmsessionHome_ptr atmsessionHome = atmsessionHome.Narrow (OBJ);

IF (atmsessionHome == null) {

CERR << "Could Not Narrow To atmsessionHome" << Endl;

Exit (1);

}

Mapping of transaction processing:

Based on CORBA-based Enterprise Bean runtime environment requires Corba's clients to participate in transaction processing involving Enterprise Beans, transaction control must be used in transactional control. When the Enterprise Bean is configured, it is possible to install according to different transaction service. The policy is defined in the configuration descriptor.

Transactional Enterprise Bean has the following definition rules: CORBA clients call the Enterprise Bean's Remote Interface and Home Interface by the Stub, which is generated by the IDL interface. If transaction is involved, it uses the interface provided by CORBA OTS.

For example: a C customer can call ATM session bean:

Try {

...

// Obtain Transaction Current

Object_ptr obj = orb-> resolve_initial_reference ("current");

Current Current = Current.Narrow (OBJ);

IF (current == null) {

CERR << "Could NOT RESOLVE CURRENT" << Endl;

Exit (1);

}

// Execute Transaction

Try {

Current-> Begin ();

AtmSession-> Transfer ("Checking", "Saving", 100.00);

Current-> Commit (0);

} catch (...) {

Current-> rollback ();

}

} catch (...) {

......

}

Safe mapping:

The security of EJB specification is mainly considering access to Enterprise Beans. CORBA specifies some methods to define identifiers, including the following:

Plain IIOP (Simple IIOP Mode): The main interface of CORBA is not accepted in early 1998. The main interface is used to determine the logo of the customer. However, the author of CORBA security services achieved different ways: GIOP. The Giop specification includes a component called a service context, in fact, an array of elements for value. Its identifier is a LONG type of CORBA, and the value is a sequence of decimal. The inlet of the service context can be used to identify the caller.

Secure IIOP: The security specification for CORBA defines an opaque data type for identity. The actual type of identity is determined by the selected security mechanism. For example: GSS Kerberos, SPKM, or CSI-ECMA.

IIOP over SSL: SSL (Encrypted Socket Protocol layer) uses X.509 to prove that the server or customer identity. When the server needs the authentication of the customer, the server uses this certificate as a customer's authentication.

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

New Post(0)