java - 使用 tomcat 打包 activeMq (java)

标签 java activemq

在我的应用程序中,我有 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;
    }
}

最佳答案

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

  • 您使用什么操作系统?
  • 您的电脑上有类似防火墙的软件吗?
  • 您能否在此提供 ActiveMQ conf 文件?
  • 你能提供一个函数吗 哪个实现了连接建立?

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

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

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

关于java - 使用 tomcat 打包 activeMq (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10928358/

相关文章:

java - "Cannot reference Employee.DEFAULT_GENDER before supertype constructor has been called"错误

java - 插入包含日期的查询格式

java - Eclipse 是否创建自动类?

java - Java 1.4 上的 ActiveMQ 客户端

java - Apache ActiveMQ : What is TCP Port 64119 Used For?

java - 如何从mysql数据库中的arraylist中添加超过50,000,000条记录

java - 使用 init.d 运行 SpringBoot Jar 时出错

spring - ActiveMQ 队列的 concurrentConsumers

java - 将 Log4J JMSAppender 与 ActiveMQ 结合使用

java - 为什么activemq会自行打开套接字