一,配置数据源
以配置MySQL数据库为例,{JBOSS_HOME}是Jboss4.0.2的安装目录
1,从{JBOSS_HOME}/docs/examples/jca下找到mysql-ds.xml,如果是MS SQL Server 就是mssql-ds.xml;
如果是oracle,就是oracle-ds.xml
2, 修改mysql-ds.xml的jndi-name,connection-url,driver-class,user-name,password元素,下面是一个例子
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: mysql-ds.xml,v 1.3.2.1 2004/12/01 11:46:00 schrouf Exp $ -->
<!-- Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://127.0.0.1:3306/scretary</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>pass</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
3,把修改好的文件拷贝到{JBOSS_HOME}/server/default/deploy下,重启Jboss。
如果没有错误的话,启动过程会输出:
10:15:40,593 INFO [WrapperDataSourceService] Bound connection factory for resource adapter for ConnectionManager 'jboss.jca:service=DataSourceBinding,name=MySqlDS to JNDI name 'java:MySqlDS'
打开控制台http://localhost:8080/jmx-console ,在jboss.jca下可以看到我们刚刚配置的数据源
name=MySqlDS,service=DataSourceBinding
name=MySqlDS,service=LocalTxCM
name=MySqlDS,service=ManagedConnectionFactory
name=MySqlDS,service=ManagedConnectionPool
4,通过Jndi名字“java:MySqlDS”就可以访问数据源了
Context ctx = null;
try {
ctx = new InitialContext();
} catch (NamingException e) {
// TODO Auto-generated catch block
log.error("InitialContext错误:" + e.getMessage());
return;
}
try {
Object obj = ctx.lookup(dsJndiName);
ds = (DataSource) obj;
} catch (Exception e) {
log.error("查找DataSource的Jndi名" + dsJndiName + "错误:"
+ e.getMessage());
}
return;
}
二 配置JMS Topic和Queue
以配置一个叫testTopic的JMS Topic和testQueue的Jms Queue为例
1,打开{JBOSS_HOME}/server/default/deploy/jms/jbossmq-destinations-service.xml文件
在</server>前后增加下面的一段:
name ="jboss.mq.destination:service=Topic,name=testTopic" >
< depends optional-attribute-name ="DestinationManager" > jboss.mq:service=DestinationManager </ depends >
< depends optional-attribute-name ="SecurityManager" > jboss.mq:service=SecurityManager </ depends >
< attribute name ="SecurityConf" >
< security >
< role name ="guest" read ="true" write ="true" />
< role name ="publisher" read ="true" write ="true" create ="false" />
< role name ="durpublisher" read ="true" write ="true" create ="true" />
</ security >
</ attribute >
</ mbean >
上面的意思是配置一个叫testTopic的Topic。可以根据自己的需要修改这个名字,其他的不用修改。
配置Queue跟Topic类似,加入下面这段:
name ="jboss.mq.destination:service=Queue,name=testQueue" >
< depends optional-attribute-name ="DestinationManager" > jboss.mq:service=DestinationManager </ depends >
< depends optional-attribute-name ="SecurityManager" > jboss.mq:service=SecurityManager </ depends >
< attribute name ="MessageCounterHistoryDayLimit" > -1 </ attribute >
< attribute name ="SecurityConf" >
< security >
< role name ="guest" read ="true" write ="true" />
< role name ="publisher" read ="true" write ="true" create ="false" />
< role name ="noacc" read ="false" write ="false" create ="false" />
</ security >
</ attribute >
</ mbean >
上面这段配置代码是配置一个叫testQueue的Queue,也可根据需要修改名字。
2,重新启动Jboss,如果没有错误可以看到
10:15:39,281 INFO [testTopic] Bound to JNDI name: topic/testTopic
10:15:39,296 INFO [testQueue] Bound to JNDI name: queue/testQueue
通过Jndi名字“java:topic/testTopic”和“java:queue/testQueue”就可以访问Topic和Queue对象了。