Slow HornetQ Producer When Queue Is Constant

I tried using a constant queue in horntQ. I made two separate examples (Producer, Consumer). My consumer works well, but the producer takes too long to finish sending the message. I worked both separately and 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(); } } 
+6
source share
1 answer

You send these messages persistently and not transactionally. Which means that each sent message must be completed individually.

This means that for each message you send, you need to make a round-trip network on the server and wait for it to complete before you can send another message.

If you had several producers in this situation, hornetq will release both manufacturers, and you will save a lot of time. (that is, the server will execute many write requests).

If you want to expedite the dispatch of a single manufacturer, you should probably use transactions.

eg:

I - change the session to a transaction:

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

II - record all N messages:

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

You can also disable the synchronization of persistent messages. (Sending them asynchronously).

+3
source

Source: https://habr.com/ru/post/896621/


All Articles