Monday, April 27, 2009

Run Glassfish v2 EJB3 example on JBoss 5

  • JBoss 5 with Derby as DataSource
There are some really good blogs about how to use derby as JBoss DataSource like Alistair Israel's, but still things to be noticed.
After copy ${JBOSS_HOME}/docs/examples/jca/derby-ds.xml to ${JBOSS_HOME}/server/default/deploy, several items should be adjusted according to the sample, like:
<jndi-name>jdbc/sample</jndi-name>
<connection-url>jdbc:derby://localhost:1527/sample</connection-url>
<driver-class>org.apache.derby.jdbc.ClientXADataSource</driver-class>
And the following to specify the database:
<mbean code="org.jboss.jdbc.DerbyDatabase" name="jboss:service=Derby">
<attribute name="Database">sample</attribute>
</mbean>
Then, Copy ${JBOSS_HOME}/docs/examples/varia/derby-plugin.jar and the ${DERBY_HOME}/lib/derby.jar and derbyclient.jar to ${JBOSS_HOME}/server/default/lib.

Next is workaround of JBoss JMX if using JBoss 5.01GA(fixed in 5.1.0GA), add -Djboss.platform.mbeanserver at the end of line in ${JBOSS_HOME}/bin/run.bat(or run.sh):
set JAVA_OPTS=...
  • JBossXBRuntimeException
You won't have it in this example, but say if you try to run the entity demo, you probably get:
DEPLOYMENTS IN ERROR:
Deployment "vfszip:.../server/default/deploy/lab-entity-demo4.jar/" is in error due to the following reason(s): org.jboss.xb.binding.JBossXBRuntimeException: Failed to resolve schema nsURI= location=persistence

This is due to the persistence.xml missing schema namespaces:
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="lab-entity-demo4">
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>

To correct it, the following should be put in persistence element:
version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  • org.jboss.deployers.spi.DeploymentException:
Required config property RequiredConfigPropertyMetaData@f1770a[name=destination descriptions=[DescriptionMetaData@7f0b4e[language=en]]] for messagingType 'javax.jms.MessageListener' not found in activation config [ActivationConfigProperty(destinationType=javax.jms.Queue)] ra=jboss.jca:service=RARDeployment,name='jms-ra.rar'

Need to add "destination" property in ActivationConfigProperty. See next step.
  • Modify MessageBean, add JBoss specific annotation:
import org.jboss.ejb3.annotation.Depends;

@MessageDriven(mappedName = "jms/NewsMsg", activationConfig = {
// JBoss by default uses AUTO_ACKNOWLEDGE:
// @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination", propertyValue="jms/NewsMsg")
})
@Depends ("jboss.mq.destination:service=Queue,name=ejbfoo")
public class NewsMessageBean implements MessageListener {
...

  • Add ejbfoo-queue-service.xml in src/conf. Netbeans will pack this file in META-INF when build the jar fiel.
<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=ejbfoo">
<attribute name="JNDIName">jms/NewsMsg</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
</server>
  • Modify persistence.xml as following:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="EjbFoo-ejbPU" transaction-type="JTA">
<!-- Toplink is default for Glassfish
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<jta-data-source>jdbc/sample</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
-->

<!-- Hibernate for JBoss -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/sample</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>

  • Modify PostNews servlet, which is the messageProducer and send the message through queue:
public class PostNews extends HttpServlet {
// For Glassfish:
//@Resource(mappedName = "jms/NewsMsgFactory")
// For JBoss:
@Resource(mappedName = "ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName = "jms/NewsMsg")
private Queue queue;
...
  • Deploy and run as you would under Glassfish.

Thursday, April 23, 2009

Play with JBoss 5: ejb3.deployer not found

Like all other open source stuffs, you have to pull the scattered pieces together before you can actually get like a simple tutorial working. Recently I downloaded JBoss 5.0.1 AS and wanted to play with the ejb3. So I downloaded the EJB3Labs from JBoss community and just as I expected, it won't run without some tweaking. Here are the tricks:
  • In build.bat, comment out set ANT_HOME=..\..\..\ant-dist and set the variable in the environment.
  • Then run build runejb, and there you get:
JBossWorldBerlinLabs\lab-slsb-demo3\src\build\build.xml:44: jboss-5.0.1.GA\server\default\deploy\ejb3.deployer not found.
  • In build.xml, you have to change the classpath as it was restructured in JBoss AS5:
deploy/ejb3.deployer -> deployers/ejb3.deployer
deploy/jboss-aop-jdk50.deployer -> deployers/jboss-aop-jboss5.deployer


Then add:
<fileset dir="${jboss.home}/common/lib">
  <include name="**/*.jar">
  </include>
</fileset>

  • Last, in Client.java
calculator = (Calculator) ctx.lookup("TODO:insert name");
should be changed to
calculator = (Calculator) ctx.lookup("CalculatorBean/remote");
Ref: JBoss EJB3 Tutorial

Thursday, April 2, 2009

Change Oracle XE default HTTP and DB ports

  • Stop listener in command window:
lsnrctl stop
  • Modify ORACLE_HOME/app/oracle/product/10.2.0/server/NETWORK/ADMIN/listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))
)
)
Change the PORT to the number wanted. Also modify PORT in tnsnames.ora and save.
  • Start listener:
lsnrctl start
  • Run SQL Plus to add local listener for that port, then change HTTP listening port:
conn system/password;
alter system set local_listener = "(ADDRESS=(PROTOCOL=TCP)(HOST=your.com)(PORT=1522))";
exec dbms_xdb.sethttpport(nnnn);
alter system register;
disc;
If "set local_listener" is omitted, you probably will get get "ORA-12514: TNS:listener does not currently know of service requested when openning SQL Plus with sqlplus system@xe

alter system register" is also important as it reregisters the DB and all services with the listener. Otherwise you can find the port is not listening with netstat -an. Also you can verify with lsnrctl status, and a line like following should be there:
...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=your.com)(PORT=8090))(Presentation=HTTP)(Session=RAW))

  • Confirm:
select dbms_xdb.gethttpport as "HTTP Port",
dbms_xdb.getftpport as "FTP Port" from dual;
Ref: http://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25107/network.htm#BHCCDEIH

Saturday, March 28, 2009

Change locale and date format of Oracle XE in SQL*Plus

Want to change the default locale and date format in SQL*Plus with Oracle XE Universal to US english. The easiest way is to add two environment variables in Windows XP:
NLS_DATE_FORMAT=DD-MON-YYYY
NLS_LANG=AMERICAN_AMERICA.AL32UTF8
Then start SQL*Plus:
C:\>sqlplus / as sysdba
SQL> select sysdate from dual;
SYSDATE
--------------------
28-MAR-2009

Monday, December 22, 2008

Change Netbeans UI Language

Before Netbeans 6.0, change ${Netbeans_HOME}/etc/netbeans.conf, add:
--locale language=en
in netbeans_default_options, and restart is OK.

For Netbeans 6.0+, this will lead to error message:
"An attempt was made to look up a localized manifest attribute named: openide-module-deprecation-message_nb_language=en

This name is in an invalid format. If you are running the Macintosh MRJ on MacOS X, make sure that your locale settings are valid. Currently: language=en
"

The new way now is to add:
-J-Duser.language=en -J-Duser.region=US
in netbeans_default_options

Ref: http://blogs.sun.com/tao/...

Convert Subversion Repositories to Mercurial

Finally found some time to migrate some projects from SVN to Hg. The enterprise SVN became so cumbersome and slow as people keep throwing all junks into it, which you have no control of.
  • The 1st recipe was trying to convert directly using Hg in Working With Subversion Repositories
    hg convert --config convert.hg.tagsbranch=0 http://marshal.com/trunk/project1
    But there seemed a bug in Hg when the SVN requires HTTP authorization - even the user and password were correct it still could not pass through:

    initializing destination project1 repository
    http authorization required
    realm: crowd
    user: marshal
    password:
    real URL is http://marshal.com/trunk/project1/
    http authorization required
    realm: crowd
    user: marshal
    password:
    http authorization required
    realm: crowd
    user: marshal
    password:
    abort: HTTP Error 401: Authorization Required

  • Now veer to the Python tool for Hg, which requires Python 2.5+ (already included on Ubuntu 8.1+) & SVN.Do the following:
    # svn not installed by default
    sudo apt-get install subversion
    # easy_install not included in Python package by default
    sudo apt-get install python-setuptools
    # download & install hgsvn
    sudo easy_install hgsvn
    Best match: hgsvn 0.1.6
    Downloading http://pypi.python.org/packages/2.5/h/hgsvn/hgsvn-0.1.6-py2.5.egg#md5=8d15f42a8f4cb8516e219b7952bd9607
    Processing hgsvn-0.1.6-py2.5.egg
    Moving hgsvn-0.1.6-py2.5.egg to /usr/lib/python2.5/site-packages
    Adding hgsvn 0.1.6 to easy-install.pth file
    Installing hgimportsvn script to /usr/bin
    Installing hgpullsvn script to /usr/bin

    Installed /usr/lib/python2.5/site-packages/hgsvn-0.1.6-py2.5.egg
    Processing dependencies for hgsvn
    Finished processing dependencies for hgsvn

  • The real staff:
    hgimportsvn http://marshal.com/trunk/project1
    * svn 'info' '--xml' 'http://marshal.com/trunk/project1'
    Password for 'marshal':


    Then the program froze after you typed the password. (Well, it happened to me).
    So I have to manually run:
    svn info http://marshal.com/trunk/project1
    And key in the user name/password there, it passed.
  • Import SVN project again:
    hgimportsvn http://marshal.com/trunk/project1
    This will import SVN's project1 into current directory and no authorization required any more.
    cd project1
    hgpullsvn
    pulls all SVN history. Then the daily use of mercurial starts...

Saturday, December 6, 2008

Ubuntu SSH How-to

Scenario: generate SSH key pairs and use PuTTY to logon to Ubuntu without inputing user & password each time.
Steps:
  • Create the Cryptographic Keys:
    $ ssh-keygen -t rsa
    Assign the pass phrase (press [enter] key twice if you don't want a passphrase). It will create 2 files in ~/.ssh directory as follows:
    ~/.ssh/id_rsa : identification (private) key
    ~/.ssh/id_rsa.pub : public key
  • Use WinSCP to copy the id_rsa (private key) to desktop, use PuTTYgen.exe to load this key and save private key to PuTTY's format.
  • Add id_rsa.pub in ~/.ssh/authorized_keys (or authorized_keys2), just in 1 line. Or simply
    mv id_rsa.pub authorized_keys
  • Be sure both the home directory and the .ssh directory be owned and writable only by the owner (700 recommended for .ssh)
  • Any error see /var/log/auth.log
  • sshd protocol 1 is insecure, so vi /etc/ssh/sshd_config:
    [...]
    Protocol 2
    PasswordAuthentication no # if want to disable interactive logon
  • Restart sshd:
    $ sudo /etc/init.d/ssh restart
  • Use the saved private key in PuTTY:
    PuTTY Configuration -> Connection -> SSH -> Auth -> Private key file for authentication,
    then load the private key. Or use PAGEANT to load this automatically.
  • Change passphrase:
    If it's already converted to PuTTY format, use PuTTYgen.exe to convert it back to OpenSSH format, then
    $ ssh-keygen -p
    Enter file in which the key is (id_rsa):...

Reference: