Here's the remote session bean facade which you can use in JBoss-3.2.x to work 
with datasources from the standalone client:

Home interface:

package org.myapp.ejb;
  | 
  | import java.rmi.RemoteException;
  | import javax.ejb.CreateException;
  | import javax.ejb.EJBHome;
  | 
  | /**
  |  * 
  |  * @author Jaikiran Pai
  |  *
  |  */
  | public interface HelloWorldHome extends EJBHome {
  | 
  |     public HelloWorldRemote create() throws RemoteException, 
CreateException;
  |     
  | }

Remote interface:

package org.myapp.ejb;
  | 
  | import java.rmi.RemoteException;
  | import javax.ejb.EJBObject;
  | 
  | /**
  |  * 
  |  * @author Jaikiran Pai
  |  *
  |  */
  | public interface HelloWorldRemote extends EJBObject {
  | 
  |     public void workWithDatabase(String dataSourceName) throws 
RemoteException;
  |     
  | }

The bean class:

package org.myapp.ejb;
  | 
  | import java.rmi.RemoteException;
  | 
  | import javax.ejb.EJBException;
  | import javax.ejb.SessionBean;
  | import javax.ejb.SessionContext;
  | import javax.naming.Context;
  | import javax.naming.InitialContext;
  | import javax.naming.NamingException;
  | import javax.sql.DataSource;
  | 
  | /**
  |  * 
  |  * @author Jaikiran Pai
  |  *
  |  */
  | public class HelloWorldBean implements SessionBean {
  |     
  |     /**
  |      * Session context
  |      */
  |     private SessionContext sessionCtx;
  |     
  |     /**
  |      * Default constructor
  |      *
  |      */
  |     public HelloWorldBean() {
  |         
  |      
  |     }
  |     
  |     /**
  |      * @see 
javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
  |      */
  |     public void setSessionContext(SessionContext sessionCtx) throws 
EJBException, RemoteException {
  |        this.sessionCtx =  sessionCtx;
  |     }
  | 
  |     public void ejbActivate() throws EJBException {
  |     
  |     }
  | 
  |     public void ejbPassivate() throws EJBException {
  |     }
  | 
  |     public void ejbRemove() throws EJBException {
  |     }
  | 
  |     
  |     public void ejbCreate() {
  |     }
  | 
  |     /**
  |      * 
  |      * @param dataSourceName
  |      * @throws RemoteException 
  |      */
  |     public void workWithDatabase(String dataSourceName) throws 
RemoteException {
  |             
  |             try {
  |                     Context ctx = new InitialContext();
  |                     DataSource dataSource = (DataSource) 
ctx.lookup(dataSourceName);
  |                     System.out.println("Got datasource: " + dataSource);
  |                     /*
  |                      * TODO: Do something with the datasource
  |                      */
  |                     
  |             } catch (NamingException ne) {
  |                     /*
  |                      * TODO: Do exception handling
  |                      */
  |                     ne.printStackTrace();
  |                     throw new RemoteException("Exception while working with 
datasource",ne);
  |             }
  |             
  |     }
  |     
  | }

ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
  | <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise 
JavaBeans 2.0//EN" 'http://java.s
  | un.com/dtd/ejb-jar_2_0.dtd'>
  | 
  | <ejb-jar >
  | 
  |    <description><![CDATA[No Description.]]></description>
  |    <display-name>Sample EJB Application</display-name>
  | 
  |    <enterprise-beans>
  | 
  |       <!-- Session Beans -->
  |       <session >
  |         
  |          <ejb-name>HelloWorldBean</ejb-name>
  |         <remote>org.myapp.ejb.HelloWorldRemote</remote>
  |         <home>org.myapp.ejb.HelloWorldHome</home>
  |          <ejb-class>org.myapp.ejb.HelloWorldBean</ejb-class>
  |          <session-type>Stateless</session-type>
  |          <transaction-type>Container</transaction-type>
  | 
  |         
  |       </session>
  | 
  | 
  |    </enterprise-beans>
  | 
  |    <!-- Assembly Descriptor -->
  |    <assembly-descriptor >
  | 
  |    <!-- transactions -->
  |    <container-transaction >
  |       <method >
  |          <ejb-name>HelloWorldBean</ejb-name>
  |           <method-name>*</method-name>
  |        </method>
  |        <trans-attribute>required</trans-attribute>
  |     </container-transaction>
  |    </assembly-descriptor>
  | 
  | 
  | 
  | </ejb-jar>

jboss.xml:

<?xml version="1.0" encoding="UTF-8"?>
  | <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.0//EN" 
"http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd";>
  | 
  | <jboss>
  |    <enterprise-beans>
  |       <session>
  |          <ejb-name>HelloWorldBean</ejb-name>
  |          <jndi-name>HelloWorld</jndi-name>
  |       </session>
  |    </enterprise-beans>
  | </jboss>

The standalone class:

package org.myapp.client;
  | 
  | import javax.naming.Context;
  | import javax.naming.InitialContext;
  | import javax.rmi.PortableRemoteObject;
  | 
  | import org.myapp.ejb.HelloWorldHome;
  | import org.myapp.ejb.HelloWorldRemote;
  | 
  | 
  | 
  | /**
  |  * 
  |  * @author Jaikiran Pai
  |  *
  |  */
  | public class TestClient {
  | 
  |     public static void main(String args[]) {
  |             try {
  |                     Context ctx = new InitialContext();
  |                                         Object obj = 
ctx.lookup("HelloWorld");                      HelloWorldHome home = 
(HelloWorldHome) PortableRemoteObject.narrow(obj,HelloWorldHome.class);
  |                     HelloWorldRemote helloWorldRemote = home.create();
  |                     
  |                                             
helloWorldRemote.workWithDatabase("java:/DefaultDS");                   
  |                     System.out.println("Done with the datasource");
  |                     
  |             } catch (Exception e) {
  |                     /*
  |                      * TODO: Do exception handling
  |                      */
  |                     e.printStackTrace();
  |             }
  |     }
  | }

You will have learn EJB to understand this. Good luck :-)



View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4024865#4024865

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4024865
_______________________________________________
jboss-user mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/jboss-user

Reply via email to