jboss4.2.3建立oracle JMS应用

一、基本配置

1 增加oracle驱动文件,ojdbc6.jar,不能使用小于该版本的jdbc驱动,jboss-4.2.3.GA\server\default\lib

2 增加retrotranslator-runtime-1.2.3.jar,jboss-4.2.3.GA\server\default\lib

3  配置数据源

jboss-4.2.3.GA\server\default\deploy\jms 增加oracle-ds.xml

4 配置JBOSS的JMS环境

拷贝jboss4.2.3\docs\examples\jms下的mysql-jdbc2-service.xml拷到jboss4.2.3\server\default\deploy\jms
下。并将数据库DataSourceBinding 改成name=OracleDS

5 增加oracle-jdbc-state-service.xml文件

修改\jboss4.2.3\server\default\deploy\jms 目录下的hsqldb-jdbc-state-service文件改名为oracle-jdbc-state-service.xml ,

并将<depends optional-attribute-name="ConnectionManager">jboss.jca:service=DataSourceBinding,name=DefaultDS</depends>

修改成name=OracleDS用于连接你的MySql的数据库。

以上配置完成之后会在oracle数据中创建如下以jms为前缀的表:

select * from  JMS_USERS;
select * from  JMS_ROLES;
select  * from  JMS_MESSAGES;

等等

二 、安全策略配置

在以上配置的基础上继续完成如下配置

1. jbossmq-destinations-service.xml 增加自定义的queue,并且引用默认的安全管理器

jboss-4.2.3.GA\server\default\deploy\jms\jbossmq-destinations-service.xml

<mbean code="org.jboss.mq.server.jmx.Queue" 
      name="jboss.mq.destination:service=Queue,name=fasQueue">
         <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="false" write="false"/>
        <role name="publisher" read="true" write="true" create="false"/>
        <role name="noacc" read="false" write="false" create="false"/>
        <role name="aaarole" read="true" write="true" create="false"/>
      </security>
    </attribute>
    </mbean>

2. 修改jbossmq-service.xml中安全域,可以使用默认的“jbossmq”

jboss-4.2.3.GA\server\default\deploy\jms\jbossmq-service.xml

<mbean code="org.jboss.mq.security.SecurityManager" name="jboss.mq:service=SecurityManager">
    <attribute name="DefaultSecurityConfig">
      <security>
        <role name="guest" read="true" write="true" create="true"/>
      </security>
    </attribute>
    <attribute name="SecurityDomain">java:/jaas/jbossmq</attribute>
    <depends>jboss.security:service=JaasSecurityManager</depends> 
    <depends optional-attribute-name="NextInterceptor">jboss.mq:service=DestinationManager</depends>
  </mbean>

3. 修改安全策略文件,增加新的安全控制策略,本文仍旧采用默认策略“jbossmq”,但是需要修改默认的数据源

server/default/conf/login-config.xml

 

<!-- Security domain for JBossMQ -->
    <application-policy name = "jbossmq">
       <authentication>
          <login-module code = "org.jboss.security.auth.spi.DatabaseServerLoginModule"
             flag = "required">
             <module-option name = "unauthenticatedIdentity">guest</module-option>
             <module-option name = "dsJndiName">java:/OracleDS</module-option>
             <module-option name = "principalsQuery">SELECT PASSWD FROM JMS_USERS WHERE USERID=?</module-option>
             <module-option name = "rolesQuery">SELECT ROLEID, ‘Roles‘ FROM JMS_ROLES WHERE USERID=?</module-option>
          </login-module>
       </authentication>
    </application-policy>

 三、测试代码

JmsSender.java

package com.demo.sender;

import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JmsSender {
    public static void main(String[] args) throws JMSException, NamingException {

        Properties env = new Properties();

        env.put(Context.PROVIDER_URL, "127.0.0.1:1099");

        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

        env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

        Context context = new InitialContext(env);

        QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("ConnectionFactory");

        //QueueConnection queueConnection = factory.createQueueConnection();
        
        QueueConnection queueConnection = factory.createQueueConnection("aaa","aaa");

        QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        
        Queue queue = (Queue) context.lookup("queue/fasQueue");

        TextMessage message = queueSession.createTextMessage();

        message.setText("hello china! 你好,中国.");

        QueueSender queueSender = queueSession.createSender(queue);

        queueSender.send(queue, message);
        System.out.println("成功发送消息!");
    }

}
View Code

JmsReaderv.java

package com.demo.reader;

import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JmsReader {
    public static void main(String[] args) throws JMSException, NamingException {

        Properties env = new Properties();
        env.put(Context.PROVIDER_URL, "localhost:1099");
        env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
        env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
        
        //连接服务器
        InitialContext ic = new InitialContext(env);

        //QueueConnection cs = ((QueueConnectionFactory) ic.lookup("ConnectionFactory")).createQueueConnection();
        
        QueueConnection cs = ((QueueConnectionFactory) ic.lookup("ConnectionFactory")).createQueueConnection("aaa", "aaa");

        Queue q = (Queue) ic.lookup("queue/fasQueue");

        QueueSession qss = cs.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

        QueueReceiver qr = qss.createReceiver(q);
        //启动链接
        cs.start();
        //开始接受,然后处理接收到的 message
        TextMessage message = (TextMessage) qr.receive();

        System.out.println(message.getText());
        System.out.println("成功读取消息");
        cs.close();
    }
}
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。