One of the development of EJB under jboss3.0-tomat4.03 (Development of SessionBean)

zhaozj2021-02-16  47

From today, I will talk about the development of EJB under JBoss3.0. First, I will tell you a very simple sessionbean start. On this basis, I will slowly say how SessionBean connects to the database, how to send Email, how to perform transaction processing, etc.

First, first you guarantee that your system can run normally and have a certain understanding of EJB.

Second, the EJB that is now said is the standard EJB is not available to the characteristics of JBoss.

1, in E: \ I built the E: / jbossejbtest / jboss / sessionbean / test directory, I wrote the following files below, one is the home interface, one is a Remote interface, one implementation bean, and EJB-JAR .xml and jboss.xml also have build.bat files and client test files, as well as RUN. BAT file, etc.

2. HOME interface file TestsessionHome.javaPackage jboss.sessionBean.test;

Import java.io.serializable;

Import java.rmi.remoteexception;

Import javax.ejb.createException;

Import javax.ejb.ejbhome;

Public Interface TestSessionHome Extends EJBHOME

{

Public TestSession Create () THROWS RemoteException, CreateException

}

3, Remote interface testsession.java

Package jboss.sessionBean.test;

Import javax.ejb.ejbobject;

Import java.rmi.remoteexception;

Public Interface TestSession Extends EJBOBJECT

{

Public string getBeanName () throws remoteException;

}

4. Implement Bean TestSessionBean.java

Package jboss.sessionBean.test;

Import java.rmi.remoteexception;

Import javax.ejb.sessionbean;

Import javax.ejb.sessionContext;

Public Class TestsessionBean Implements SessionBean

{

Public String getBeanName () THROWS RemoteException

{

System.out.println ("Someone Called` GetBeanName! 'Method ");

Return "this ejb's nameis sessionbean test";

Public testsessionBean () {}

Public void ejbcreate () {}

Public void ejbremove () {}

Public void ejbactivate () {}

Public void ejbpassiVate () {}

Public void setsessionContext (sessioncontext sc) {}

}

The above is three most basic documents, very simple. But a simplest test is OK.

Next is two profiles

1, ejb-jar.xml configuration file

JBoss EJB TEST

Test

TestSession

jboss.sessionBean.test.TestSessionHome

jboss.sessionBean.test.TestSession

jboss.sessionBean.test.TestSessionBean

stateless

Bean

2, jboss.xml configuration file

TestSession

testsessionHomejndi // This is the JNDI name called on the client.

The following is a build file, mainly packaged to generate JAR files. Usually I like to use Ant tools, but it is easy to use for very common, so I wrote the form of the BAT file. Build.bat

Set java_home = e: /jdk1.3.1

Set jboss_home = e: /jboss3.0_tomcat4.03

@echo.

@echo ============== Building jbossejb ============

@ echo.set jdk_classes =% java_home% / lib / Tools.jar

Set classpath =.;% jdk_classes%;% jboss_home% / server / default / lib / jboss-j2ee.jar

SET PATH =% java_home% / bin;% PATH%

Set javac =% java_home% / BIN / JAVAC

MKDIR EJBTEMP

Mkdir EJBTEMP / META-INF

% Javac% -d ejbtemp * .java

Copy meta-inf / *. XML EJBTEMP / META-INF

CD EJBTEMP

JAR VCF Testsssion.jar JBoss / SessionBean / Test / *. Class Meta-INF

Jar -tvf testsession.jar

CD ..

@echo on

@echo.

After running this batch file, generate an EJBTEMP directory in the E: / jbossejbtest / jboss / sessionBean / Test / JBoss / sessionBean / Test directory with a TestSession.jar file, which will be released automatically in the server / default / deploy directory.

The following is two files for the client test,

TestClient.java file, this is a Java file in the form of an application, and I will test through various clients such as JSP, servlet.

Import javax.naming. *;

Import javax.rmi.portableremoteObject;

Import jboss.sessionBean.test. *;

Import java.util. *;

Public class testclient {

Public TestClient () {

}

Public static void main (String [] args) {

Hashtable hash = new hashtable ();

Hash.put ("java.naming.factory.initial", "org.jnp.interfaces.namingContextFactory";

Hash.Put ("Java.naming.Provider.URL", "LocalHost: 1099");

Try {

InitialContext CONTEXT = New InitialContext (HASH);

Object TestObject = Context.lookup ("TestSessionHomejndi);

TestsessionHome TestSessionHome = (TestsessionHome) PortableRemoteObject.Narrow (TestObject, TestSessionHome.Class);

Testsession TestSession = TestSessionHome.create ();

System.out.println ("EJB Name:" TestSession.getBeanName ());

}

Catch (Exception E)

{

System.out.println (E.TOString ());

}

}

}

Batch file run.bat file running and compiled

Set java_home = e: /jdk1.3.1

Set jboss_home = e: /jboss3.0_tomcat4.03

Set program_home = E: / jbossejbtest / jboss / sessionbean / test

@ echo ============== building jbossejb ============

@echo.

Set jdk_classes =% java_home% / lib / Tools.jar

Set classpath =% jdk_classes%;% program_home% / ejbtemp;% jboss_home% / client / jbossx-client.jar;% jboss_home% / client / jboss-client.jar;% jboss_home% / client / log4j.jar;% jboss_home /server/default/lib/jboss-j2ee.jar;%jboss_home%/client/jnp-client.jar;%jboss_home%/client/jnet.jar;%jboss_Home%/client/jboss-common-client.jar;

SET PATH =% java_home% / bin

Set javac =% java_home% / BIN / JAVAC

Set java =% java_home% / bin / java

% Javac% testclient.java

Set classpath =% classpath% ;.

% Java% TestClient

@echo on

@echo.

The above is the file tested by this basic test. When you test, you can modify some paths such as java_home; jboss_home; program_home, etc., this is a very simple test sessinbean's stuff, I will be The above basis will slowly expand, expand many of its use. Ok, I will come here today.

The SessionBean connection database is actually basically the same in JSP.

First, refer to the previous configuration library configuration, create a database connection service.

My configuration file is as follows (Oracle)

Oracleds

JDBC: Oracle: Thin: @cuipeng: 1521: ORA8

Oracle.jdbc.driver.OracleDriver

system

manager

jboss.jca: service = rarDeployment, name = jboss localtransaction jdbc wrapper

0

50

15

bycontainer

jboss.jca: service = CachedConnectionManager jboss.security:service=JaasSecurityManager

Java: / TransactionManager

jboss.jca: service = rarDeployer

Second, in front of the remote interface file TestSession.java Add the following method, of course, the name can be customized.

Public Collection getdbtablename () throws remoteException;

Third, the implementation of the method in the interface in the implementation of the TestSessionBean.java is added to the interface.

Public Collection getDbtablename () THROWS RemoteException

{

Vector temp = new vector ();

System.out.Println ("JBoss SessionBean Connection DB Test");

Try {

InitialContext CTX = New InitialContext ();

DataSource DS = (DataSource) CTX.lookup ("java: / oracleds"); // and the previous corresponding

Connection conn = ds.getConnection ();

Statement Stmt = conn.createstatement ();

ResultSet RS = Stmt.executeQuery ("SELECT * from Tab");

While (rs.next ()) {

Temp.addeElement (Rs.getstring ("TNAME"));

}

CONN.CLOSE ();

} catch (exception e) {

System.out.println (e);

}

Return Temp;

}

Fourth, in the TestClient.java TestClient.java in TestClient.java is as follows:

Import javax.naming. *;

Import javax.rmi.portableremoteObject;

Import jboss.sessionBean.test. *;

Import java.util. *;

Public class testclient {

Public TestClient () {

}

Public static void main (String [] args) {

Hashtable hash = new hashtable ();

Hash.put ("java.naming.factory.initial", "org.jnp.interfaces.namingContextFactory";

Hash.put ("java.naming.provider.URL", "localhost: 1099"); TRY {

InitialContext CONTEXT = New InitialContext (HASH);

Object TestObject = Context.lookup ("TestSessionHomejndi);

TestsessionHome TestSessionHome = (TestsessionHome) PortableRemoteObject.Narrow (TestObject, TestSessionHome.Class);

Testsession TestSession = TestSessionHome.create ();

System.out.println ("EJB Name:" TestSession.getBeanName ());

Collection col = testsession.getdbtablename ();

Iterator it = col.iterator ();

While (it.hasnext ()) {

String Tablename = (String) it.next ();

System.out.println (TableName);

}

}

Catch (Exception E)

{

System.out.println (E.TOString ());

}

}

}

The rest of the files do not need to be modified, first run the build.bat file, then publish the generated JAR file, then run the Client side to test run.bat

Connect to the email delivery in SessionBean!

First, refer to the configuration of the email with javamail, build a connection.

My Mail-Service.xml file is as follows:

Archives = "mail.jar, activation.jar, mail-plugin.jar" />

Name = "jboss: service = mail">

Java: / Testjbossmail

lacklhl // username

Password // Password

It is a free mailbox for 21cn, but it needs to be verified.

Second, the front remote interface file TestSession.java adds one of the following ways, of course, the name can be customized.

Public String Sendmail (String from, String To, String Subject, String Content) THROWS RemoteException;

Third, the TestsessionBean.java in the achieved bean is as follows.

Package jboss.sessionBean.test;

Import java.rmi.remoteexception;

Import javax.ejb.sessionbean;

Import javax.ejb.sessionContext;

Import javax.naming. *;

Import javax.sql. *;

Import java.sql. *;

Import java.util. *;

Import javax.mail. *;

Import javax.mail.internet. *;

Public Class TestsessionBean Implements SessionBean

{

InitialContext CTX;

Public String getBeanName () THROWS RemoteException

{

System.out.println ("Someone Called` GetBeanName! 'Method ");

Return "this EJB's Nameis sessionBean Test";

}

Public Collection getDbtablename () THROWS RemoteException

{

Vector temp = new vector ();

System.out.Println ("JBoss SessionBean Connection DB Test");

Try {

DataSource DS = (Datasource) CTX.lookup ("Java: / ORACLEDS");

Connection conn = ds.getConnection ();

St.createstatement (); ResultSet RS = Stmt.executeQuery ("SELECT * from Tab");

While (rs.next ()) {

Temp.addeElement (Rs.getstring ("TNAME"));

}

CONN.CLOSE ();

} catch (exception e) {

System.out.println (e);

}

Return Temp;

}

Public String Sendmail (String from, String To, String Subject, String Content) THROWS RemoteException

{

String Temp;

Try {

Session sessions = (session) CTX.lookup ("java: / testjbossmail");

MIMEMESSAGE MSG = New MimeMessage (sessions);

Msg.SetFrom (New InternetDress (from));

Msg.setRecipients (javax.mail.Message.RecipientType.to, To);

Msg.setsubject (Subject);

Msg.setsentdate (New java.util.date ());

Multipart multipt = new mimemultipart ();

MIMEBODYPART MSGBODY = new mimebodypart ();

Msgbody.setContent (Content, "Text / Plain");

Multipt.addbodypart (msgbody);

Msg.setContent (MULTIPT);

Transport.send (MSG);

Temp = "Sendmail OK!";

System.out.println ("Sendmail OK!");

}

Catch (Exception E)

{

Temp = "error:" E;

E.PrintStackTrace ();

}

Return Temp;

}

Public testsessionBean () {

}

Public void ejbcreate () {

Try {

CTX = New InitialContext ();

} catch (exception e) {

System.out.println (e);

}

}

Public void ejbremove () {}

Public void ejbactivate () {}

Public void ejbpassiVate () {}

Public void setsessionContext (sessioncontext sc) {}

}

Fourth, build. BAT file changes as follows

Set java_home = e: /jdk1.3.1

Set jboss_home = e: /jboss3.0_tomcat4.03

@echo.

@echo ============== Building jbossejb ============

@echo.

Set jdk_classes =% java_home% / lib / Tools.jarset classpath =.;% jdk_classes%;% jboss_home% / server / default / lib / jboss-j2ee.jar;% jboss_home% / server / default / lib / mail.jar

SET PATH =% java_home% / bin;% PATH%

Set javac =% java_home% / BIN / JAVAC

MKDIR EJBTEMP

Mkdir EJBTEMP / META-INF

% Javac% -d ejbtemp testession * .java

Copy * .xml EJBTEMP / META-INF

CD EJBTEMP

JAR VCF Testsssion.jar JBoss / SessionBean / Test / *. Class Meta-INF

Jar -tvf testsession.jar

CD ..

@echo on

@echo.

5. Modifications in TestClient.java in TestClient.java in TestClient.java is as follows:

Import javax.naming. *;

Import javax.rmi.portableremoteObject;

Import jboss.sessionBean.test. *;

Import java.util. *;

Public class testclient {

Public TestClient () {

}

Public static void main (String [] args) {

Hashtable hash = new hashtable ();

Hash.put ("java.naming.factory.initial", "org.jnp.interfaces.namingContextFactory";

Hash.Put ("Java.naming.Provider.URL", "LocalHost: 1099");

Try {

InitialContext CONTEXT = New InitialContext (HASH);

Object TestObject = Context.lookup ("TestSessionHomejndi);

TestsessionHome TestSessionHome = (TestsessionHome) PortableRemoteObject.Narrow (TestObject, TestSessionHome.Class);

Testsession TestSession = TestSessionHome.create ();

// Test Method

System.out.println ("EJB Name:" TestSession.getBeanName ());

// Test DB

Collection col = testsession.getdbtablename ();

Iterator it = col.iterator ();

While (it.hasnext ()) {

String Tablename = (String) it.next ();

System.out.println (TableName);

}

// Test Mail

String Temp = TestSession.sendmail ("lacklhl@21cn.com", "lacklhl@21cn.com", "test sessionbean mail", "test sessionbean mail"; system.out.println (TEMP);

}

Catch (Exception E)

{

System.out.println (E.TOString ());

}

}

}

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

New Post(0)