当队列持久时,慢速HornetQ Producer [英] Slow HornetQ Producer when Queue is persistent

查看:153
本文介绍了当队列持久时,慢速HornetQ Producer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在horntQ中尝试过Persistent Queue。我做了两个单独的例子(Producer,Consumer)。我的消费者工作得很好,但制作人花了太多时间来完成发送消息。我既分开也分开跑。可能是什么问题呢?
我的代码是:

I have tried with Persistent Queue in horntQ. I have made two separate examples (Producer, Consumer). My consumer is working well but the Producer is taking too much time to finish sending message. I have run both separately as well as together. What could be the problem? my code is:

public  class HornetProducer implements Runnable{

    Context ic = null;
    ConnectionFactory cf = null;
    Connection connection =  null;
    Queue queue = null;
    Session session = null;
    MessageProducer publisher =  null;
    TextMessage message = null;
    int messageSent=0;

     public synchronized static Context getInitialContext()throws javax.naming.NamingException {

            Properties p = new Properties( );
            p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
            p.put(Context.URL_PKG_PREFIXES," org.jboss.naming:org.jnp.interfaces");
            p.put(Context.PROVIDER_URL, "jnp://localhosts:1099");

            return new javax.naming.InitialContext(p);
        }  

    public HornetProducer()throws Exception{            

        ic = getInitialContext();
        cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
        queue = (Queue)ic.lookup("queue/testQueue2");
        connection = cf.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);        
        publisher = session.createProducer(queue);
        connection.start();

    }

    public void publish(){      
        try{        

            message = session.createTextMessage("Hello!");
            System.out.println("StartDate: "+new Date());

            for(int i=0;i<10000;i++){                   
                 messageSent++;              
                 publisher.send(message);                
            }
            System.out.println("EndDate: "+new Date());
        }catch(Exception e){
            System.out.println("Exception in Consume: "+ e.getMessage());
        }           
    }

    public void run(){
         publish();
    }

    public static void main(String[] args) throws Exception{

        new HornetProducer().publish();    
    }

}


推荐答案

您持续发送这些消息,非事务性发送。什么意思,发送的每条消息都必须单独完成。

You are sending these messages persistently, and non transactionally. What means, each message sent has to be completed individually.

这意味着对于你发送的每条消息,你必须进行一次网络往返服务器,并等待它在你发送另一条消息之前完成持久性。

That means for each message you send, you have to make a network round trip to the server, and wait it finish persistency before you can send another message.

如果在这种情况下有多个生产者,hornetq会批处理两个生产者,你会节省很多时间。 (即服务器将批量许多写请求)。

If you had multiple producers on this situation, hornetq would batch both producers and you would save a lot of time. (i.e. the server will batch many write requests).

如果你想加快发送单个生产者的速度,你应该使用交易。

If you want to speed up the sending of a single producer, you should use transactions probably.

例如:

I - 将会话更改为transactioned:

I - Change your session to transactioned:

session = connection.createSession(true, Session.SESSION_TRANSACTIONED); 

II - 提交每N条消息:

II - commit every N messages:

   for(int i=0;i<10000;i++){                   
         messageSent++;              
         publisher.send(message);  
         if (messageSent % 1000 == 0) session.commit();              
    }
    session.commit();

您还可以在持久消息上禁用同步。 (异步发送)。

You could also disable sync on Persistent messages. (Sending them asynchronously).

这篇关于当队列持久时,慢速HornetQ Producer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆