1

在我的应用程序中,我有 activeMq 将消息从客户端发送到服务器,反之亦然。我将它作为独立服务器运行。因此,当客户端机器发送消息时,消息在 activeMq 队列中传递,然后由服务器检索(我的应用程序)当且仅当事务在本地完成,这意味着客户端计算机和服务器(我的应用程序)位于同一台计算机中。但是,当我从两台不同的计算机上运行客户端和服务器时,一台服务器和另一台客户端的客户端只能建立与服务器的连接,但消息不会传递到 activeMq 队列。我认为这是 activeMq 问题。

谁能告诉我如何解决这个问题?谢谢

这是将客户端发送的数据传递到队列的代码。

package event.activeMq;  
import java.util.ArrayList;  
import java.util.Arrays;  
import java.util.Date;  
import java.util.Iterator;  

import javax.jms.Connection;  
import javax.jms.DeliveryMode;  
import javax.jms.Destination;  
import javax.jms.MessageProducer;  
import javax.jms.Session;  
import javax.jms.TextMessage;  

import org.apache.activemq.ActiveMQConnection;  
import org.apache.activemq.ActiveMQConnectionFactory;  
import org.apache.activemq.console.command.store.amq.CommandLineSupport;  
import org.apache.activemq.util.IndentPrinter;  

public class ProducerTool extends Thread {

    private Destination destination;
    private int messageCount = 1;
    private long sleepTime;
    private boolean verbose = true;
    private int messageSize = 1000;
    private static int parallelThreads = 1;
    private long timeToLive;
    private String user = ActiveMQConnection.DEFAULT_USER;
    private String password = ActiveMQConnection.DEFAULT_PASSWORD;
    private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    private String subject = "CLOUDD.DEFAULT";
    private boolean topic;
    private boolean transacted;
    private boolean persistent;
    private static Object lockResults = new Object();
    private static String DateTime="";
    private static String TaskID="";
    private static String UniqueEventID="";
    private static String Generator="";
    private static String GeneratorBuildVsn="";
    private static String Severity="";
    private static String EventText="";
    private static String SubsystemID="";
    private static String EventNumber="";
    private static String atmId="";


   public void element(String[] element) {  
       this.DateTime = element[0];  
       this.TaskID = element[1];  
       this.Generator = element[2];  
       this.Severity = element[3];  
       this.EventText = element[4];  
       this.SubsystemID = element[5];  
       this.EventNumber = element[6];  
       this.GeneratorBuildVsn = element[7];  
       this.UniqueEventID = element[8];  
       this.atmId = element[9];  
   }  
    public static void main(String[] args) {  
        System.out.println("came here");  
        ArrayList<ProducerTool> threads = new ArrayList();  
        ProducerTool producerTool = new ProducerTool();  
        producerTool.element(args);  

        producerTool.showParameters();  
        for (int threadCount = 1; threadCount <= parallelThreads; threadCount++) {  
            producerTool = new ProducerTool();  
            CommandLineSupport.setOptions(producerTool, args);  
            producerTool.start();  
            threads.add(producerTool);  
        }  

        while (true) {  
            Iterator<ProducerTool> itr = threads.iterator();  
            int running = 0;  
            while (itr.hasNext()) {  
                ProducerTool thread = itr.next();  
                if (thread.isAlive()) {  
                    running++;  
                }  
            }
            if (running <= 0) {
                System.out.println("All threads completed their work");
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
        }
    }

    public void showParameters() {
        System.out.println("Connecting to URL: " + url);
        System.out.println("Publishing a Message with size " + messageSize + " to " + (topic ? "topic" : "queue") + ": " + subject);
        System.out.println("Using " + (persistent ? "persistent" : "non-persistent") + " messages");
        System.out.println("Sleeping between publish " + sleepTime + " ms");
        System.out.println("Running " + parallelThreads + " parallel threads");

        if (timeToLive != 0) {
          //  System.out.println("Messages time to live " + timeToLive + " ms");
        }
    }

    public void run() {
        Connection connection = null;
        try {
            // Create the connection.
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
            connection = connectionFactory.createConnection();
            connection.start();

            // Create the session
            Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
            if (topic) {
                destination = session.createTopic(subject);
            } else {
                destination = session.createQueue(subject);
            }

            // Create the producer.
            MessageProducer producer = session.createProducer(destination);
            if (persistent) {
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            } else {
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            }
            if (timeToLive != 0) {
                producer.setTimeToLive(timeToLive);
            }

            // Start sending messages
            sendLoop(session, producer);

           // System.out.println("[" + this.getName() + "] Done.");

            synchronized (lockResults) {
                ActiveMQConnection c = (ActiveMQConnection) connection;
               // System.out.println("[" + this.getName() + "] Results:\n");
                c.getConnectionStats().dump(new IndentPrinter());
            }

        } catch (Exception e) {
           // System.out.println("[" + this.getName() + "] Caught: " + e);
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (Throwable ignore) {
            }
        }
    }  

    protected void sendLoop(Session session, MessageProducer producer) throws Exception {

        for (int i = 0; i < messageCount || messageCount == 0; i++) {

            TextMessage message = session.createTextMessage(createMessageText(i));
            if (verbose) {
                String msg = message.getText();
                if (msg.length() > 50) {
                    msg = msg.substring(0, 50) + "...";
                }
              //  System.out.println("[" + this.getName() + "] Sending message: '" + msg + "'");
            }

            producer.send(message);

            if (transacted) {
              //  System.out.println("[" + this.getName() + "] Committing " + messageCount + " messages");
                session.commit();
            }
            Thread.sleep(sleepTime);
        }
    }

    private String createMessageText(int index) {
        StringBuffer buffer = new StringBuffer(messageSize);

        buffer.append("DateTime "+DateTime+" EventNumber "+EventNumber+" TaskID "+TaskID+" AtmId "+atmId+
                " Generator "+Generator+" GeneratorBuildVsn "+GeneratorBuildVsn+" Severity "+Severity+
                " UniqueEventID "+UniqueEventID+" EventText "+EventText+" SubsystemID "+SubsystemID+" End ");
        if (buffer.length() > messageSize) {
            return buffer.substring(0, messageSize);
        }
        for (int i = buffer.length(); i < messageSize; i++) {
            buffer.append(' ');
        }

        DateTime="";
        EventNumber="";
        TaskID="";
        atmId="";
        Generator="";
        GeneratorBuildVsn="";
        Severity="";
        UniqueEventID="";
        EventText="";
        SubsystemID="";

        return buffer.toString();        
    }

    public void setPersistent(boolean durable) {
        this.persistent = durable;
    }

    public void setMessageCount(int messageCount) {
        this.messageCount = messageCount;
    }

    public void setMessageSize(int messageSize) {
        this.messageSize = messageSize;
    }

    public void setPassword(String pwd) {
        this.password = pwd;
    }

    public void setSleepTime(long sleepTime) {
        this.sleepTime = sleepTime;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setTimeToLive(long timeToLive) {
        this.timeToLive = timeToLive;
    }

    public void setParallelThreads(int parallelThreads) {
        if (parallelThreads < 1) {
            parallelThreads = 1;
        }
        this.parallelThreads = parallelThreads;
    }

    public void setTopic(boolean topic) {
        this.topic = topic;
    }

    public void setQueue(boolean queue) {
        this.topic = !queue;
    }

    public void setTransacted(boolean transacted) {
        this.transacted = transacted;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }
}
4

1 回答 1

0

您需要使用以下问题的答案更新您的问题:

  • 你用什么操作系统?
  • 你的电脑上有防火墙之类的软件吗?
  • 你能在这里提供 ActiveMQ conf 文件吗?
  • 你能提供一个实现连接建立的功能吗?

upd:我不明白你所有的逻辑,但我认为这里有错误:

try {
    Thread.sleep(1000);
    } catch (Exception e){    
    e.printStackTrace();
    }

并且永远永远永远不会捕获所有异常!这是非常危险的。如果你想捕获异常,你需要处理它。

于 2012-06-07T08:55:52.460 回答