My EJB learning course (six)

zhaozj2021-02-12  139

Oh, everyone saw the QUGHOST that overself returned to my post? He's my good friend.

He has a lot of funny stories, I discuss with him, ready to send his funny experience in a new piece, but I don't know if he agrees.

OK, the words are passed down. I will always end the third chapter soon, in fact, the third chapter of the session bean to 5 is already finished, but there is a very critical thing inside, that is, serialization.

Serialization

The Java serialization mechanism can output the status of the entire object to a stream

Objects can then rebuild from its original state

In fact, it is very simple, it is to output all things of an object to a file. It can also be something else. many.

Then when you want to use him, you will then find some of this object's status and so on.

What really happened?

The instance of the bean is not serialized. If the Bean instance itself is serialized, a new instance needs to be created when it is re-built. This is lost.

Bean's state is serialized each attribute needs to be considered separately

What will be serialized?

A sequential (serializable) object

NULL

Direct reference to a specific inquirable object:

A reference to a bean home or Remote interface

Reference to sessionContext

Serialization of direct references to insequential partial parts

Containers must save their REMOTE and HOME interfaces when they are passivated.

Perhaps relying on Java's java.io.objectOutputStream and object replacement techniques in java.io.objectInputStream

Partial sequence

Each properties of the constituent state must be sequentially

The content of all attributes must contain sequentially

Helper class

Otherwise, the container can choose to recover the bean instance when passivated

These all I copied from IBM. I hope to understand the serialization of everyone.

Give me my program, I use it or the EJB of the state session bean.

/ *

* Create a date 2004-7-5

*

* Change the generated file template for

* Window> Preferences> Java> Code Generation> Code and Notes

* /

Package com.testing.client;

Import java.rmi.remoteexception;

Import java.util.hashtable;

Import javax.ejb.createException;

Import javax.naming.initialcontext;

Import javax.naming.namingexception;

Import com.test.session. *;

Import java.io. *;

Import javax.ejb.handle;

/ **

* @Author Sy

*

* Change the template for the generated type annotation

* Window> Preferences> Java> Code Generation> Code and Notes

* /

Public class handletest {

Private com.test.session.StateFulhome gethome () throws namingexception {

return (com.test.session.statefulhome) getContext (). Lookup (

com.test.session.StateFulHome.jndi_name);

}

Private InitialContext getContext () throws namingexception {

Hashtable props = new hashtable ();

Props.put

InitialContext.Initial_Context_Factory,

"Org.jnp.interfaces.namingContextFactory"; PrOPS.PUT (InitialContext.Provider_URL, "JNP: //127.0.0.1: 1099");

// this Establishes the security for authorization / authentication

// Props.Put (InitialContext.security_principal, "username");

// Props.Put (InitialContext.security_credentials, "password");

InitialContext InitialContext = New InitialContext (PROPS);

Return INIALCONTEXT;

}

Public void testbean () {

Try {

Com.test.Session.Stateful MyBean = gethome (). Create (1000);

//System.out.println ("adding 1000 ");

MyBean.Addfunds (1000);

System.out.println ("Adding 1000");

System.out.println ("Now Account IS:" MyBean.getBalance ());

MyBean.withdrawfunds (100);

System.out.Println ("withdrawing 100");

System.out.println ("Now Account IS:" MyBean.getBalance ());

System.out.println ("Saving Handle");

SaveHandle (MyBean);

System.out.Println ("Saving Success");

MyBean = NULL;

System.out.Println ("Loading Handle");

MyBean = loadingHandle ();

System.out.Println ("Loading Success");

System.out.println ("Now Account IS:" MyBean.getBalance () ", WITHDRAWING 100");

MyBean.withdrawfunds (100);

System.out.println ("Now Account IS:" MyBean.getBalance ());

// --------------------------------------

// this is the place you make your caver

//System.out.println (MyBean.callyourthod ());

} catch (remoteException E) {

E.PrintStackTrace ();

} catch (createException e) {

E.PrintStackTrace ();

} catch (namingexception e) {

E.PrintStackTrace ();

}

}

Public static void main (String [] args) {

Handletest Test = new handletest ();

Test.TestBean ();

}

Private static void savehandle (stateful mybean) {try {

FileOutputStream Fos = New FileOutputStream ("Symic");

ObjectOutputStream out = New ObjectOutputStream (FOS);

Out.writeObject (myBean.getHandle ());

Out.close ();

} catch (exception e) {}

}

Private static stateful loadhandle () {

Try {

FileInputStream Fis = New FileInputStream ("Symic");

ObjectInputStream in = New ObjectInputStream (FIS);

Handle Handle = (Handle) in.readObject ();

Stateful mybean = (stateful) Handle.GeteJBObject ();

In.Close ();

Return mybean;

} catch (exception x) {}

Return NULL;

}

}

In fact, it is very simple, look at my procedure, then find the Symic file, see what you wrote inside. Haha ~~~~

Say something else, I have recently found it is really a good country. But it seems that there is no chance to go all over China!

ITaly

SHOPPER's DESTINATION!

It is very suitable in his buying! I heard that Prada, Amani is there a lot of TAX Rebate, only half of the price, and things are all the most Top Class.

I really want to go ~~~~~~~~~~

ITaly

My Dream-Pradise!

Ha ha! ~~~

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

New Post(0)