0. Prerequisite
1) Download Weblogic based sample code from following URL, and extract to your directory.
https://www.manning.com/books/ejb3-in-action
My directory is $HOME/student/ejbactGF
2) Add environment variables in your environment:
export JAVA_HOME=/opt/jdk
export AS_INSTALL_PARENT=~/glassfish4
export AS_INSTALL=$AS_INSTALL_PARENT/glassfish
export PATH=$JAVA_HOME/bin:$AS_INSTALL_PARENT/bin:$AS_INSTALL/bin:$PATH
3) Start glassfish domain and derby
$ asadmin start-domain
Waiting for domain1 to start ............
Successfully started the domain : domain1
domain Location: /home/weblogic/glassfish4/glassfish/domains/domain1
LogFile: /home/weblogic/glassfish4/glassfish/domains/domain1/logs/server.log
AdminPort: 4848
Command start-domain executed successfully.
$ asadmin start-database
Starting database in Network Server mode on host 0.0.0.0 and port 1527.
…
Starting database in the background.
Log redirected to /home/weblogic/student/ejbactGF/derby.log.
Commands tart-database executed successfully.
4) Create db instance if need
#!/bin/sh
java \
-cp "$JAVA_HOME/db/lib/derby.jar:$JAVA_HOME/db/lib/derbynet.jar:$JAVA_HOME/db/lib/derbytools.jar:$JAVA_HOME/db/lib/derbyoptionaltools.jar:$JAVA_HOME/db/lib/derbyclient.jar" \
-Dij.driver=org.apache.derby.jdbc.ClientDriver \
-Dij.protocol=jdbc:derby: -D"ij.database=//localhost:1527/sun-appserv-samples;create=true" \
-Dij.user=app -Dij.password=app \
org.apache.derby.tools.ij
To make things easy, here I use the default database sun-appserv-samples, which related to following JDBC resources in glassfish:
Connectionpool – DerbyPool
Resource– jdbc/__default
1. Customize general configuration
$ cd $HOME/student/ejbactGF
$ vi common.xml # customize general configure for all chapters
$ diff -u common.xml.orig common.xml
...
<!--** Customize as per your environment **** -->
- <property name="J2EE_HOME" value="${env.J2EE_HOME}"/>
+ <property name="J2EE_HOME" value="${env.AS_INSTALL}"/>
<propertyname="JAVA_HOME" value="${env.JAVA_HOME}"/>
…
- <property name="ASADMIN" value="asadmin.bat"/>
- <property name="WSIMPORT" value="wsimport.bat"/>
- <property name="APPCLIENT" value="appclient.bat"/>
+ <property name="ASADMIN" value="asadmin"/>
+ <property name="WSIMPORT" value="wsimport"/>
+ <property name="APPCLIENT" value="appclient"/>
+ <property name="derby.driver" value="org.apache.derby.jdbc.ClientDriver" />
+ <property name="derby.url" value="jdbc:derby://localhost:1527/sun-appserv-samples;create=true;ServerName=localhost;databaseName=sun-appserv-samples"/>
+ <property name="derby.username" value="app"/>
+ <property name="derby.password" value="app"/>
...
+ <target name="db-setup" depends="init"description="Database setup for Derby">
+ <sql driver="${derby.driver}"
+ url="${derby.url}"
+ userid="${derby.username}"password="${derby.password}"
+ οnerrοr="continue"
+ print="yes"
+ src="sql/tables.sql"
+ classpath="${JAVA_HOME}/db/lib/derbyclient.jar"/>
+ </target>
+
+ <target name="drop-que">
+ <exec executable="${J2EE_HOME}/bin/${ASADMIN}"failοnerrοr="true" vmlauncher="false">
+ <arg line=" --host ${admin.host}" />
+ <arg line=" --port ${admin.port}" />
+ <arg line=" delete-jms-resource" />
+ <arg line="${jms.queue}" />
+ </exec>
+ </target>
+
+ <target name="crt-que">
+ <exec executable="${J2EE_HOME}/bin/${ASADMIN}"failοnerrοr="true" vmlauncher="false">
+ <arg line=" --host ${admin.host}" />
+ <arg line=" --port ${admin.port}" />
+ <arg line=" create-jms-resource" />
+ <arg line=" --restype javax.jms.Queue" />
+ <arg line="${jms.queue}" />
+ </exec>
+ </target>
2. Customize chapter1
$ cd $HOME/student/ejbactGF/chapter1
$ for i in `find * -name "*.orig"`; do j=`expr $i :'\(.*\).orig'`; echo diff -u $i $j; diff -u $i $j; echo; echo; done
diff-u build.xml.orig build.xml
...
level="verbose"></echo>
- <echo message="asadmin.bat undeploy --user ${admin.user}--password ${admin.password} --host ${admin.host} --port${admin.port} chapter1"></echo>
+ <echo message="asadmin undeploy --user ${admin.user}--password ${admin.password} --host ${admin.host} --port${admin.port} chapter1"></echo>
<exec executable="${J2EE_HOME}/bin/${ASADMIN}" failοnerrοr="false">
...
- <echo message="asadmin.bat deploy --user ${admin.user}--password ${admin.password} --host ${admin.host} --port${admin.port} --upload=true lib/chapter1.ear"></echo>
+ <echo message="asadmin deploy --user ${admin.user}--password ${admin.password} --host ${admin.host} --port${admin.port} --upload=true lib/chapter1.ear"></echo>
<exec executable="${J2EE_HOME}/bin/${ASADMIN}" failοnerrοr="false">
3. Build, deploy and run
$ cd $HOME/student/ejbactGF/chapter1
$ ant clean build
$ ant deploy
$ ant run
...
run:
[echo] Executing client class using/home/weblogic/glassfish4/glassfish/bin/appclient
[exec] Invoked EJB successfully .. see server console for output
From server log we can see:
Hello Curious George welcome to EJB 3 In Action!