简体   繁体   English

activeMQ生产者需要长时间发送消息

[英]activeMQ producer taking long time in sending messages

I have written a producer application that enqueue JMS messages by using Executer Service in activeMQ and it is working finely but the problem is it's taking long time to enqueue messages . 我编写了一个生产者应用程序,该程序通过在activeMQ中使用Executer Service来排队JMS消息,并且运行良好,但问题是排队消息需要很长时间。

there are three files: 1. ExecutePushServer.java 2. ActiveMQProducer.java 3. SendPush.java 这里有三个文件:1. ExecutePushServer.java 2. ActiveMQProducer.java 3. SendPush.java

ExecutePushServer.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 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 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);
}

}

please help me !! 请帮我 !!

The first thing I'd look at is your thread usage; 我要看的第一件事是您的线程使用情况; you're creating a new thread for each message, which could definitely be a big part of why your performance isn't faster. 您正在为每条消息创建一个新线程,这绝对可能是为什么您的性能没有提高的很大一部分。 Why can't you have your threads run a loop that sends messages till they run out of messages to send, and then have N threads that split the work? 您为什么不能让线程运行一个循环来发送消息,直到它们用完了要发送的消息,然后又有N个线程来拆分工作?

You'll also probably want to run your reader logic in a separate thread, where it reads the file as fast as it can and hands off the things it reads to the threads, so you don't have to wait while the file is read to even get started. 您可能还需要在单独的线程中运行阅读器逻辑,在该线程中,它会尽可能快地读取文件,并将读取的内容交给线程,因此您不必等待读取文件的过程。甚至开始。 Make sure you make the data structures that you use to pass data between the reader threads and the message threads are thread-safe! 确保您使用于在阅读器线程之间传递数据的数据结构和消息线程是线程安全的!

Once you do that, if the speed isn't where you want it to be, look at the broker's configuration. 完成此操作后,如果速度不是您想要的速度,请查看代理的配置。 (And post it here, if you want someone to look at it.) In particular, if your messages are persistent, then look at where they're being persisted and see if there are other options that would be faster. (如果您希望有人查看,请在此处发布。)特别是,如果您的消息是持久性的,则请查看它们的持久性,并查看是否有其他选择会更快。 (JDBC storage is generally the slowest persistence option, so consider other options.) Or you might even be able to make your messages non-persistent to make them faster; (JDBC存储通常是最慢的持久性选项,因此请考虑其他选项。)或者,您甚至可以使消息不是持久的,从而使它们更快。 you'll have to decide if you can live with that trade-off. 您必须决定是否可以接受这种折衷。 Figure out whether your messages are being passed asynchronously or synchronously; 确定您的消息是异步传递还是同步传递; if sync, you might want to enable async instead. 如果同步,则可能要启用异步。 And make sure that producer flow control isn't kicking in (check the broker logs); 并确保没有启动生产者流控制(检查代理日志); if it is, then your consumers are probably too slow and are slowing down your producers. 如果是这样,那么您的消费者可能太慢了,并且正在减慢您的生产者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActiveMq Producer需要更长的时间将JMS消息发送到目标队列 - ActiveMq Producer is taking longer time to send JMS message to Destination queue ActiveMQ不接收来自生产者的消息 - ActiveMQ Not Receiving messages from Producer ActiveMQ不会使消息脱离队列 - ActiveMQ Not Taking Messages off Queue 无法创建生产者在 ActiveMQ 上发送消息 - Unable to create producer to send messages on ActiveMQ 由生产者排队并由消费者出队的消息的activemq日志记录 - activemq logging of messages enqueued by producer and dequeued by consumer ActiveMQ代理关闭。 生产者异步发送消息。 如何对生产者发送的邮件执行重新传递? 如何使用死信队列 - ActiveMQ Broker down. Producer sending messages asynchronously. How to perform re delivery for messages send by Producer? How to use dead letter queue ActiveMQ生产者从PostgreSQL表中的内容发送消息 - ActiveMQ producer sending message from content inside PostgreSQL's table Producer/Consumer Spring 发送多条消息 - Producer/Consumer Spring sending multiple messages 在将消息发送到ActiveMQ之前先对其进行池化 - Pooling messages before sending them to ActiveMQ 如果使用者未确认消息,ActiveMQ将停止向队列使用者发送消息 - ActiveMQ stops sending messages to Queue Consumer in case of consumer not acknowledging messages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM