首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jboss eap 7-通过资源适配器将消息发布到IBM MQ

jboss eap 7-通过资源适配器将消息发布到IBM MQ
EN

Stack Overflow用户
提问于 2021-04-06 05:07:24
回答 1查看 289关注 0票数 0

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

代码语言:javascript
复制
/subsystem=resource-adapters/resource-adapter=ibm-mq-resource-adapter:add(archive=wmq.jmsra-9.0.4.0.rar, transaction-support=NoTransaction)
代码语言:javascript
复制
/subsystem=resource-adapters/resource-adapter=ibm-mq-resource-adapter/admin-objects=queue-ao1:add(class-name=com.ibm.mq.connector.outbound.MQQueueProxy, jndi-name=java:jboss/outbound)

/subsystem=resource-adapters/resource-adapter=ibm-mq-resource-adapter/admin-objects=queue-ao1/config-properties=baseQueueName:add(value=TEST1)
/subsystem=resource-adapters/resource-adapter=ibm-mq-resource-adapter/admin-objects=queue-ao1/config-properties=baseQueueManagerName:add(value=TESTMANAGER)

连接定义:

代码语言:javascript
复制
<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>

根据我的理解,如果我从连接工厂mqSeriesJMSFactoryoutbound向出站队列发送一条消息,我应该能够到达IBM MQ。我试着用下面的代码来查找连接工厂,但是我得到了命名notfound异常。请帮帮忙

代码语言:javascript
复制
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) {
                }
            }
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2021-04-09 21:17:12

对上面的内容做了几处修改。

使用ManagedQueueConnectionFactoryImpl而不是com.ibm.mq.connector.outbound.ManagedConnectionFactoryImpl,在连接定义中使用

  1. 以避免运行时出现类强制转换异常。

RA创建的

  1. 连接工厂不能从它的JVM外部访问。编写了一个servlet来访问这些连接工厂。我可以用下面的代码连接。

代码语言:javascript
复制
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);
            }
            
          }
        
        
        
        
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66959796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档