簡體   English   中英

activeMQ生產者需要長時間發送消息

[英]activeMQ producer taking long time in sending messages

我編寫了一個生產者應用程序,該程序通過在activeMQ中使用Executer Service來排隊JMS消息,並且運行良好,但問題是排隊消息需要很長時間。

這里有三個文件:1. ExecutePushServer.java 2. ActiveMQProducer.java 3. SendPush.java

ExecutePushServer.java:

package com.rh.pushserver;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

public class ExecutePushServer {

/**
 * @uthor ankit
 */

static int maxThread = 0;
static BufferedReader br = null;
static String fileLocation = null;
static List<String> tokenList = new ArrayList<String>();
private static String txt;
static Properties configFile = new Properties();
private final static Logger logger = Logger
        .getLogger(ExecutePushServer.class.getName());

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        configFile.load(ExecutePushServer.class.getClassLoader()
                .getResourceAsStream("config.properties"));
        maxThread = Integer.valueOf(configFile.getProperty("POOL_SIZE"));

        fileLocation = configFile.getProperty("LOCATION");

        txt = configFile.getProperty("txt");
        logger.info("Message text is : " + txt);

        br = new BufferedReader(new FileReader(fileLocation));

        ActiveMQProducer mqProducer = new ActiveMQProducer();

        tokenList = getList(br);
        logger.info("tokenList created.");


        ExecutorService executor = Executors.newFixedThreadPool(maxThread);
        for (String id : tokenList) {
            Runnable work = new SendPush(mqProducer, id);
            executor.execute(work);
        }

        // This will make the executor accept no new threads
        // and finish all existing threads in the queue
        logger.info("All Ids Entered in Pool.");
        executor.shutdown();

        while (!executor.awaitTermination(10, TimeUnit.MINUTES)) {
            logger.info("Inside awaitTermination");
        }

        mqProducer.closeConnection();

    } catch (IOException e) {
        logger.error("Error in Reading File" + e);

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        logger.error("Error in termination of executer" + e);
    } finally {
        try {
            if (br != null)
                br.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

private static List<String> getList(BufferedReader br) {
    // TODO Auto-generated method stub
    String currentLine;
    try {
    while ((currentLine = br.readLine()) != null) {
        tokenList.add(currentLine);
    }

    return tokenList;

    } catch (IOException e) {
        logger.error("Error occured in creating tokenList !" + e);
        return null;
    } 
}

}

ActiveMQProducer.java

package com.rh.pushserver;


import java.io.IOException;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;

public class ActiveMQProducer {

/**
 * @uthor ankit  
 */


private final String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private final String subject = "PUSH_NOTIFICATION";
private Connection connection;
private Session session;
private String txt=null;
private MessageProducer producer;
private MapMessage mapMessage;
static Properties configFile = new Properties();
private final static Logger logger=Logger.getLogger(ActiveMQProducer.class.getName());

public ActiveMQProducer() {
    try {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        connection = connectionFactory.createConnection();
        connection.start();

        logger.info("Connection Created.");

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(subject);
        producer = session.createProducer(destination);

        logger.info("Producer generated");

               configFile.load(ActiveMQProducer.class.getClassLoader().getResourceAsStream("config.properties"));

        txt=configFile.getProperty("txt");

        mapMessage = session.createMapMessage();
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        logger.error("Error JMS Exception occured in creating connection"+e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        logger.error("Exception occured while opening file "+e);
    }
}
public MessageProducer getProducer() {
    return producer;
}

public void enqueueMessage(String id){
    try {   
        mapMessage.setString("ID", id);
        mapMessage.setString("DISPLAY_STRING", txt);
        mapMessage.setInt("BADGE_COUNT", 1);
        mapMessage.setString("DEVICE_TYPE", "ANDROID");

        producer.send(mapMessage);
        logger.info("Sent on : "+id);

    } catch (JMSException e) {
        // TODO Auto-generated catch block
        logger.error("Error while Enqueue"+e);
    }
}

public void closeConnection(){
    try {
        connection.close();
        logger.info("Connection closed");
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        logger.error("Error in connection closer"+e);
    }
}

}

SendPush.java

package com.rh.pushserver;



public class SendPush implements Runnable {
/**
 * @uthor ankit
 */

private String id;
private ActiveMQProducer mqProducer;


public SendPush(ActiveMQProducer mqProducer,String id) {

    this.id=id;
    this.mqProducer=mqProducer;
}

@Override
public void run() {

    mqProducer.enqueueMessage(id);
}

}

請幫我 !!

我要看的第一件事是您的線程使用情況; 您正在為每條消息創建一個新線程,這絕對可能是為什么您的性能沒有提高的很大一部分。 您為什么不能讓線程運行一個循環來發送消息,直到它們用完了要發送的消息,然后又有N個線程來拆分工作?

您可能還需要在單獨的線程中運行閱讀器邏輯,在該線程中,它會盡可能快地讀取文件,並將讀取的內容交給線程,因此您不必等待讀取文件的過程。甚至開始。 確保您使用於在閱讀器線程之間傳遞數據的數據結構和消息線程是線程安全的!

完成此操作后,如果速度不是您想要的速度,請查看代理的配置。 (如果您希望有人查看,請在此處發布。)特別是,如果您的消息是持久性的,則請查看它們的持久性,並查看是否有其他選擇會更快。 (JDBC存儲通常是最慢的持久性選項,因此請考慮其他選項。)或者,您甚至可以使消息不是持久的,從而使它們更快。 您必須決定是否可以接受這種折衷。 確定您的消息是異步傳遞還是同步傳遞; 如果同步,則可能要啟用異步。 並確保沒有啟動生產者流控制(檢查代理日志); 如果是這樣,那么您的消費者可能太慢了,並且正在減慢您的生產者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM