首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jboss 5到jboss 7 jms

jboss 5到jboss 7 jms
EN

Stack Overflow用户
提问于 2013-03-04 17:01:14
回答 1查看 2.6K关注 0票数 1

我在同一台机器上运行jboss5和JBOSS7,所以我更改了端口号(JBOSS5-4040和jboss7-8080)。我需要从jboss 5向jboss 7发送一条消息。我读了一篇文章,声明必须使用JMS桥。

代码语言:javascript
复制
https://community.jboss.org/wiki/BridgeJMSMessagesFromAS5ToAS7

我在jboss 5中创建了一个队列,并通过独立程序向队列发送消息。

代码语言:javascript
复制
import java.io.Console;
import java.util.Properties;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class sender {
    String url_;
    String name_;
    Connection conn = null;
    Session session = null;
    Queue queue = null;

    public sender(String url, String name) throws JMSException,
            NamingException {

        url_ = url;
        name_ = name;

        this.initializeSender();
    }

    private void initializeSender() throws JMSException, NamingException {

        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial",
                "org.jnp.interfaces.NamingContextFactory");
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");

        props.setProperty("java.naming.provider.url", "jnp://localhost:1099");

        Context context = new InitialContext(props);

        ConnectionFactory cf = (ConnectionFactory) context
                .lookup("java:/ConnectionFactory");

        conn = cf.createConnection();

        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

        queue = (Queue) context.lookup(name_);

        conn.start();

    }

    public void send(String text) throws JMSException, NamingException {
        // Send a text msg
        MessageProducer producer = session.createProducer(queue);
        TextMessage tm = session.createTextMessage(text);
        producer.send(tm);
        producer.close();
    }

    public void disconnect() throws JMSException {
        if (conn != null) {
            conn.stop();
        }

        if (session != null) {
            session.close();
        }

        if (conn != null) {
            conn.close();
        }
    }

    public String getTopicName() {
        return name_;
    }

    public String getTopicURL() {
        return url_;
    }

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

        sender sender = new sender("remote://localhost:4447", "BalaQueue");

        sender.send("My Message");
        sender.disconnect();


    }

}

我在jboss 7中创建了一个队列,并通过独立程序向队列发送消息。

代码语言:javascript
复制
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;

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;

public class QSender {
    public static void main(String[] args) {
        new QSender().send();
    }

    public void send() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        try {
            // Strings for JNDI names
            String factoryName = "jms/RemoteConnectionFactory";
            String queueName = "jms/queue/ram";
            // Create an initial context.
            Properties props = new Properties();
            props.put(Context.INITIAL_CONTEXT_FACTORY,
                    "org.jboss.naming.remote.client.InitialContextFactory");
            props.put(Context.PROVIDER_URL, "remote://localhost:4447");
            props.put(Context.SECURITY_PRINCIPAL, "ramguest");
            props.put(Context.SECURITY_CREDENTIALS, "password");
            InitialContext context = new InitialContext(props);

            QueueConnectionFactory factory = (QueueConnectionFactory) context
                    .lookup(factoryName);
            Queue queue = (Queue) context.lookup(queueName);
            context.close();
            // Create JMS objects
            QueueConnection connection = factory.createQueueConnection(
                    "ramguest", "password");
            QueueSession session = connection.createQueueSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            QueueSender sender = session.createSender(queue);

            System.out.println("Enter message to send or 'quit' to quit.");
            String messageText = null;
            while (true) {
                messageText = reader.readLine();
                if ("quit".equalsIgnoreCase(messageText)) {
                    break;
                }
                TextMessage message = session.createTextMessage(messageText);
                sender.send(message);
            }
            // Exit
            reader.close();
            System.out.println("Exiting...");
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

和我的桥文件(jboss-service.xml)

代码语言:javascript
复制
<server>    
    <loader-repository>com.example:archive=unique-archive-name
        <loader-repository-config>java2ParentDelegation=false</loader-repository-config>
    </loader-repository>    <!-- AS7 JMS Provider -->    
    <mbean code="org.jboss.jms.jndi.JMSProviderLoader" name="jboss.messaging:service=JMSProviderLoader,name=RemoteJBossMQProvider">     <attribute name="ProviderName">RemoteXAConnectionFactory</attribute>       
        <attribute name="ProviderAdapterClass">org.jboss.jms.jndi.JNDIProviderAdapter</attribute>
        <attribute name="FactoryRef">jms/RemoteConnectionFactory</attribute>
        <attribute name="QueueFactoryRef">jms/RemoteConnectionFactory</attribute>
        <attribute name="TopicFactoryRef">jms/RemoteConnectionFactory</attribute>
        <attribute name="Properties">         java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory  

            java.naming.provider.url=remote://localhost:4447
            java.naming.security.principal=ramguest 
            java.naming.security.credentials=password 
        </attribute>
    </mbean>    
    <mbean code="org.jboss.jms.server.bridge.BridgeService" name="jboss.jms:service=Bridge,name=LegayBridgeSend" xmbean-dd="xmdesc/Bridge-xmbean.xml">            
        <depends optional-attribute-name="SourceProviderLoader">jboss.messaging:service=JMSProviderLoader,name=JMSProvider</depends>
        <depends optional-attribute-name="TargetProviderLoader">jboss.messaging:service=JMSProviderLoader,name=RemoteJBossMQProvider</depends>
        <attribute name="SourceDestinationLookup">BalaQueue</attribute>  
        <attribute name="TargetDestinationLookup">java:jboss/exported/jms/queue/ram</attribute>
        <attribute name="QualityOfServiceMode">1</attribute>
        <attribute name="MaxBatchSize">1</attribute>
        <attribute name="MaxBatchTime">-1</attribute>
        <attribute name="FailureRetryInterval">10000</attribute>
        <attribute name="MaxRetries">-1</attribute>
        <attribute name="AddMessageIDInHeader">false</attribute>    
        <attribute name="TargetUsername">ramguest</attribute>
        <attribute name="TargetPassword">password</attribute>   
    </mbean> 
</server> 

当我启动jboss5时,我得到异常

代码语言:javascript
复制
WARN  [Bridge] jboss.jms:name=LegayBridgeSend,service=Bridge Failed to set up connections
javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
        at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1678)
        at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1795)
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:693)
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at org.jboss.jms.server.bridge.JNDIFactorySupport.createObject(JNDIFactorySupport.java:66)
        at org.jboss.jms.server.bridge.JNDIDestinationFactory.createDestination(JNDIDestinationFactory.java:45)
        at org.jboss.jms.server.bridge.Bridge.setupJMSObjects(Bridge.java:953)
        at org.jboss.jms.server.bridge.Bridge.setupJMSObjectsWithRetry(Bridge.java:1223)
        at org.jboss.jms.server.bridge.Bridge.access$1600(Bridge.java:68)
        at org.jboss.jms.server.bridge.Bridge$FailureHandler.run(Bridge.java:1569)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.net.SocketTimeoutException: Receive timed out
        at java.net.PlainDatagramSocketImpl.receive0(Native Method)
        at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:145)
        at java.net.DatagramSocket.receive(DatagramSocket.java:725)
        at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1647)

有谁能指引我吗..。

EN

回答 1

Stack Overflow用户

发布于 2015-01-15 01:44:47

JBOSS AS 7不支持JNP,请使用jboss远程处理

代码语言:javascript
复制
<attribute name="Properties">
      java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory  
      java.naming.provider.url=remote://localhost:4447
      java.naming.security.principal=ramguest 
      java.naming.security.credentials=password 
</attribute>

变化

代码语言:javascript
复制
org.jnp.interfaces.NamingContextFactory

代码语言:javascript
复制
org.jboss.naming.remote.client.InitialContextFactory
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15197827

复制
相关文章

相似问题

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