用tomcat封装activeMq(java)

时间:2012-06-07 08:35:25

标签: 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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

UPD:    我不明白你的所有逻辑,但我在这里犯了错误:

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

永远不会永远不会遇到所有异常!这很危险。 如果你想捕获异常,你需要处理它。