jboss eap 7-使用资源适配器向IBM MQ发布消息

jboss eap 7-使用资源适配器向IBM MQ发布消息,jboss,ibm-mq,redhat,jboss-eap-7,wildfly-14,Jboss,Ibm Mq,Redhat,Jboss Eap 7,Wildfly 14,我已经将WMQ JMS资源适配器(9.0.4)安装到我的JBOSS EAP 7 standalone-full.xml,并创建了连接工厂和它的管理对象 /subsystem=resource-adapters/resource-adapter=ibm-mq-resource-adapter:add(archive=wmq.jmsra-9.0.4.0.rar, transaction-support=NoTransaction) 连接定义: <connection-definition c

我已经将WMQ JMS资源适配器(9.0.4)安装到我的JBOSS EAP 7 standalone-full.xml,并创建了连接工厂和它的管理对象

/subsystem=resource-adapters/resource-adapter=ibm-mq-resource-adapter:add(archive=wmq.jmsra-9.0.4.0.rar, transaction-support=NoTransaction)
连接定义:

<connection-definition class-name="com.ibm.mq.connector.outbound.ManagedConnectionFactoryImpl" jndi-name="java:jboss/mqSeriesJMSFactoryoutbound" tracking="false" pool-name="mq-cd">
 <config-property name="channel">
                 SYSTEM.DEF.XXX
</config-property>
                            <config-property name="hostName">
                                XX-XXX
                            </config-property>
                            <config-property name="transportType">
                                CLIENT
                            </config-property>
                            <config-property name="queueManager">
                                TESTMANAGER
                            </config-property>
                            <config-property name="port">
                                1414
                            </config-property>
                        </connection-definition>

SYSTEM.DEF.XXX
XX-XXX
客户
测试经理
1414
据我所知,如果我从连接工厂mqSeriesJMSFactoryoutbound向出站队列发布消息,我应该能够到达IBM MQ。我试图用下面的代码查找连接工厂,但我发现命名未找到异常。请帮忙

public class TestQueueConnection {

    // Set up all the default values
    private static final String DEFAULT_MESSAGE = "Hello, World! successfull";
    private static final String DEFAULT_CONNECTION_FACTORY = "java:jboss/mqSeriesJMSFactoryoutbound";
    private static final String DEFAULT_DESTINATION = "java:jboss/outbound";
    private static final String DEFAULT_MESSAGE_COUNT = "1";
    private static final String DEFAULT_USERNAME = "jmsuser";
    private static final String DEFAULT_PASSWORD = "jmsuser123";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "http-remoting://127.0.0.1:8070";

    public static void main(String[] args) throws JMSException {

        Context namingContext = null;

        try {
             String userName = System.getProperty("username", DEFAULT_USERNAME);
             String password = System.getProperty("password", DEFAULT_PASSWORD);

            // Set up the namingContext for the JNDI lookup
            final Properties env = new Properties();
            env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
            namingContext = new InitialContext(env);

            // Perform the JNDI lookups
            String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
             namingContext.lookup(connectionFactoryString);
            QueueConnectionFactory connectionFactory = (QueueConnectionFactory) 
            JMSContext jmsContext = connectionFactory.createContext(DEFAULT_USERNAME, DEFAULT_PASSWORD);
    

            Queue destination = (Queue) namingContext.lookup(DEFAULT_DESTINATION);
            
            jmsContext.createProducer().send(destination, DEFAULT_MESSAGE);
            System.out.println("><><><><><><>< MESSAGE POSTED <><><><><><><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" );
            
        } catch (NamingException e) {
            e.printStackTrace();
        } finally {
            if (namingContext != null) {
                try {
                    namingContext.close();
                } catch (NamingException e) {
                }
            }
        }
    }
公共类TestQueueConnection{
//设置所有默认值
private static final String DEFAULT_MESSAGE=“Hello,World!successfull”;
私有静态最终字符串DEFAULT_CONNECTION_FACTORY=“java:jboss/mqSeriesJMSFactoryoutbound”;
私有静态最终字符串DEFAULT_DESTINATION=“java:jboss/outbound”;
私有静态最终字符串默认\u消息\u COUNT=“1”;
私有静态最终字符串DEFAULT_USERNAME=“jmsuser”;
私有静态最终字符串DEFAULT_PASSWORD=“jmsuser123”;
私有静态最终字符串INITIAL\u CONTEXT\u FACTORY=“org.jboss.naming.remote.client.InitialContextFactory”;
私有静态最终字符串提供程序\u URL=“http-remoting://127.0.0.1:8070";
公共静态void main(字符串[]args)引发JMSException{
Context namingContext=null;
试一试{
字符串userName=System.getProperty(“用户名”,默认用户名);
字符串password=System.getProperty(“password”,默认密码);
//为JNDI查找设置namingContext
最终属性env=新属性();
环境放置(Context.INITIAL\u Context\u工厂,INITIAL\u Context\u工厂);
put(Context.PROVIDER\u URL,System.getProperty(Context.PROVIDER\u URL,PROVIDER\u URL));
namingContext=新的初始上下文(env);
//执行JNDI查找
String connectionFactoryString=System.getProperty(“connection.factory”,默认连接工厂);
查找(connectionFactoryString);
QueueConnectionFactory connectionFactory=(QueueConnectionFactory)
JMSContext JMSContext=connectionFactory.createContext(默认用户名、默认密码);
队列目的地=(队列)namingContext.lookup(默认目的地);
jmsContext.createProducer().send(目标,默认消息);
System.out.println(“>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>”;
}捕获(NamingE例外){
e、 printStackTrace();
}最后{
if(namingContext!=null){
试一试{
namingContext.close();
}捕获(NamingE例外){
}
}
}
}

对上述内容进行了几处更改

  • 在连接定义中,不是com.ibm.mq.connector.outbound.ManagedConnectionFactoryImpl,而是使用ManagedQueueConnectionFactoryImpl来避免运行时发生类强制转换异常

  • RA创建的连接工厂在其JVM之外是不可访问的。我编写了一个servlet来访问这些连接工厂。我可以使用下面的代码进行连接


  • 您需要在其中包含导出名称的连接工厂,以便从外部访问它。因此,您应该将其绑定到java:jboss/exported/mqSeriesJMSFactoryoutbound
    public class TestQueueConnection {
    
        // Set up all the default values
        private static final String DEFAULT_MESSAGE = "Hello, World! successfull";
        private static final String DEFAULT_CONNECTION_FACTORY = "java:jboss/mqSeriesJMSFactoryoutbound";
        private static final String DEFAULT_DESTINATION = "java:jboss/outbound";
        private static final String DEFAULT_MESSAGE_COUNT = "1";
        private static final String DEFAULT_USERNAME = "jmsuser";
        private static final String DEFAULT_PASSWORD = "jmsuser123";
        private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
        private static final String PROVIDER_URL = "http-remoting://127.0.0.1:8070";
    
        public static void main(String[] args) throws JMSException {
    
            Context namingContext = null;
    
            try {
                 String userName = System.getProperty("username", DEFAULT_USERNAME);
                 String password = System.getProperty("password", DEFAULT_PASSWORD);
    
                // Set up the namingContext for the JNDI lookup
                final Properties env = new Properties();
                env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
                env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
                namingContext = new InitialContext(env);
    
                // Perform the JNDI lookups
                String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
                 namingContext.lookup(connectionFactoryString);
                QueueConnectionFactory connectionFactory = (QueueConnectionFactory) 
                JMSContext jmsContext = connectionFactory.createContext(DEFAULT_USERNAME, DEFAULT_PASSWORD);
        
    
                Queue destination = (Queue) namingContext.lookup(DEFAULT_DESTINATION);
                
                jmsContext.createProducer().send(destination, DEFAULT_MESSAGE);
                System.out.println("><><><><><><>< MESSAGE POSTED <><><><><><><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" );
                
            } catch (NamingException e) {
                e.printStackTrace();
            } finally {
                if (namingContext != null) {
                    try {
                        namingContext.close();
                    } catch (NamingException e) {
                    }
                }
            }
        }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.getWriter().append("Served at: ").append(request.getContextPath());
            
            Context namingContext = null;
            String connectionFactoryString = "mqSeriesJMSFactoryoutbound";
            String queueName = "outbound";
            
            MessageProducer producer = null;
            Session session = null;
            Connection conn =null;
            try {
                namingContext = new InitialContext();
                
                QueueConnectionFactory connectionFactory = (QueueConnectionFactory) namingContext.lookup(connectionFactoryString);
                Queue destination = (Queue) namingContext.lookup(queueName);
                
                conn = connectionFactory.createConnection();
                session = conn.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
                producer = session.createProducer(destination);
                TextMessage message = session.createTextMessage();
                message.setText(msg);
                
                producer.send(message,
                        Message.DEFAULT_DELIVERY_MODE,
                        Message.DEFAULT_PRIORITY,
                        Message.DEFAULT_TIME_TO_LIVE);
                
                          
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            finally {
                
                // Close the message producer
                try {
                  if (producer != null) producer.close();
                }
                catch (JMSException e) {
                  System.err.println("Failed to close message producer: " + e);
                }
                
                // Close the session
                try {
                  if (session != null) session.close();
                }
                catch (JMSException e) {
                  System.err.println("Failed to close session: " + e);
                }
                
                // Close the connection
                try {
                    if(conn != null)
                  conn.close();
                }
                catch (JMSException e) {
                  System.err.println("Failed to close connection: " + e);
                }
                
              }
            
            
            
            
        }