JBoss EJB simple instance

zhaozj2021-02-16  69

JBoss is a free EJB container that is loved by everyone, following a simple CMP session small example for your reference.

This bean provides a user's basic information for a CMP entity bean and a session bean entity bean for a userBean

The user has two fields email and password, where Email is the primary key, in order to prevent the client from directly accessing the entity bean, the entity bean

Do not provide remote interface, but through a sessionBean to operate entity bean, the client passes the interaction of sessionbean

Operating entity bean's purpose

Environment: JBoss3.23 WinXP / 2000

Database: mysql for win alpha 4.0

JDBC driver: mysql alpha3.1

More than you have found, don't you have

1: JBoss configuration

Workers must be a prostitute, first to teach our protagonist:

Unzip the downloaded JBoss3.23 to a directory, in order to save things, I put it in the C: / jboss directory

After setting the Java_Home variable, set the JBoss home directory: C: / jboss

After setting these variables, run $ JBOSS_HOME / BIN / Run.BAT

The following character interface appears, indicating that the configuration is successful

13: 13: 56,816 INFO [Server] JBoss (MX Microkernel) [3.2.3 (build: cvstag = jboss_3

_2_3 Date = 200311301445)] Started in 36s: 993ms

2: Configuring data sources

1. First copy mysql's JDBC driver to $ jboss_home / server / default / lib directory

Then copy the file c: /jboss/docs/examples/jca/mysql-ds.xml to $ jboss_home / server / default / deploy

Under contents,

2. Use the text editing tool to open the mysql-ds.xml file, edit the content:

The sample is as follows:

mysqlds

JDBC: MySQL: // Dell: 3306 / JBossDB

Org.gjt.mm.mysql.driver

x

y

Please fill in the user name and password according to the specific settings of the database, and other do not move

3. Modify $ jboss_home / server / default / conf / standardjbosscmp-jdbc.xml

file:

The sample is as follows:

Java: / mysqlds

mysql

Don't change the rest

After the configuration is complete, the basic environment is OK, and the BEAN is started.

Three: Development Entity Bean

1. Local Home interface provides a way to create a bean, which provides a CREATE method, and a FindByPrimaryKey method

The detailed list is as follows:

Package test;

Import javax.ejb.ejblocalhome;

Import javax.ejb.createException;

Import javax.ejb.finderexception;

public interface LocalUserHome extends EJBLocalHome {public LocalUser create (String email, String password) throws CreateException; public LocalUser findByPrimaryKey (String email) throws FinderException;} 2.local services interface provides methods detailed list follows: package test; import javax.ejb.EJBLocalObject ; Public Interface LocalUser Extends EjblocalObject {

Public string getemail (); public string getpassword ();

} Should be read-only for Email and Password, so there is no setter method, but the setter method is required in the specific bean 3. The entity bean class implements business methods and callback methods. The detailed list is as follows: package test; import javax.ejb. EntityBean; import javax.ejb.createException; import javax.ejb.entityContext; public abstract class userbean imports entitybean {

public String ejbCreate (String email, String password) throws CreateException {this.setEmail (email); this.setPassword (password); return null;} // abstract method public abstract void setEmail (String email); public abstract String getEmail () ; public abstract void setPassword (String password); public abstract String getPassword (); public void ejbPostCreate (String email, String password) {} ​​public void ejbActivate () {} public void ejbPasstivate () {} public void ejbLoad () {} Public void ejbstore () {} public void setentityContext (entityContext CTX) {} public void unsentityContext ()} public void ejbremove () {}

} 4. EJB-jar.xml configuration file list: Userbean Userbean

Test.localUserHome test.localuser test.userbean

Container java.lang.string true 2.x

Userbean

No Description Password no description Email

Email

4: Developing SessionBean 1.SessionBean Home interface, because Interact with customers, so SessionBean must provide a list of remote interfaces as follows: package test;

Import java.rmi.RemoteException; import javax.ejb.createException; import javax.ejb.ejbhome

Public interface userManagementHome Extends ejbhome {

Public UserManagement Create () throws createException, RemoteException;

Note that the interface needs to throw RemoteException and the local interface does not require a 2.SessionBean remote interface, which provides the list of logic methods required to implement as follows: package test;

Import javax.ejb.ejbobject; import java.rmi.RemoteException;

public interface UserManagement extends EJBObject {public void addUser (String email, String password) throws RemoteException; public void removeUser (String email) throws RemoteException; public boolean verifyPassword (String email, String password) throws RemoteException;

} 3 SessionBean's implementation class detailed list: Package Test;

import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.CreateException; import javax.ejb.RemoveException; import javax.ejb.FinderException; import javax.naming.Context; import javax.naming.NamingException; import Javax.naming.initialcontext; import test.localuser; import test.localHome

Public Class UserManagementBean Implements SessionBean {

private LocalUserHome userHome = null; // helper method is used to find the home interface entity bean private LocalUserHome getUserHome () throws NamingException {Context initial = new InitialContext (); return (LocalUserHome) initial.lookup ( "java: comp / env / ejb / Localuser ");

Public void adduser (string email, string password {try {localuser user = usrhome.create (email, password);} catch (createExcect) {system.out.print (e.tostring ());}

}

Public void removeuser (String email) {

Try {userhome.remove (email);} catch (transoveexception e) {system.out.println (e.tostring ());}

}

public boolean verifyPassword (String email, String password) {boolean verify = false; try {LocalUser user = userHome.findByPrimaryKey (email); verify = user.getPassword () equals (password);.} catch (FinderException e) {System. Out.println (e.tostring ());} Return Verify;

Public void ejbcreate () throws createException {

Try {userhome = this.getuserHome ();} catch (namingexception e) {system.out.println (e.tostring ());}

}

Public void ejbactivate () {} {try {userhome = getUserHome ();} catch (namingexception e) {system.out.print ("Unable to find local user in activate);}

}

Public void ejbremove () {}

Public void ejbpassivate () {userhome = null;

}

Public void setsessionContext (sessionContext CTX) {}

} 4: Configuration description file for sessionbean: UserManagementBean UserManagementBean

Test.userManagementHome Test.userManagement Test.userManagementBean

stateful container

EJB / LOCALUSER Entity test.localUserhome < / local-home> test.localuser Userbean

5: Client Client Package Test;

Import Test.useMAGEmentHome; import test.user manufacture;

Import javax.naming.context; import javax.naming.initialcontext; import javax.rmi.portableremoteObject;

Public class client {

public static void main (String [] args) {try {Context ctx = new InitialContext (); Object ref = ctx.lookup ( "UserManagementHome"); UserManagementHome home = (UserManagementHome) PortableRemoteObject.narrow (ref, UserManagementHome.class); UserManagement user = home.create (); user.adduser ("wuchensir@yahoo.com.cn", "lms"); User.Adduser ("wuchensir@sohu.com", "sohu"); user.adduser (" Wuchensir@163.com "," 163 "); User.Removeuser (" wuchensir@163.com "); boolean login = user.verifypassword (" wuchensir@sohu.com "," sohu "); system.out.println (login);} catch (exception e) {system.out.print (e.tostring ());}}}

Six: JBoss configuration file jboss.xml list is as follows:

Userbean localuser UserManagementBean UserManagementHome ejb / localuser localuser This list provides JNDI settings for entity beans and sessionBeans, a SESSIONBEAN's reference description of entity beans

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

New Post(0)