JBoss ActiveMQ Topic RedeliveryDelay 太小

JBoss ActiveMQ Topic RedeliveryDelay too small

我使用 JBoss 和默认的 ActiveMQ 来向一些订阅了主题的客户端发送消息。不幸的是 onMessage(Message message) 只为一条消息被多次调用。

JNDI 查找:

private static void lookupRemoteTopic() throws NamingException, JMSException
{
    final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
    final String DEFAULT_DESTINATION = "jms/topic/refresh";
    final String DEFAULT_USERNAME = "ejb";
    final String DEFAULT_PASSWORD = "ejbSuperSecret";
    final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    final String PROVIDER_URL = "http-remoting://192.168.2.72:8080";

    final Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
    env.put(Context.PROVIDER_URL, PROVIDER_URL);
    env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);
    env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);
    InitialContext namingContext = new InitialContext(env);

    // Perform the JNDI lookups
    TopicConnectionFactory connectionFactory = (TopicConnectionFactory) namingContext.lookup(DEFAULT_CONNECTION_FACTORY);
    Topic destination = (Topic) namingContext.lookup(DEFAULT_DESTINATION);

    TopicConnection con = connectionFactory.createTopicConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);        
    con.start();
    TopicSession session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    TopicSubscriber sub = session.createSubscriber(destination);
    sub.setMessageListener(this);
}

onMessage:

public void onMessage(Message message)
{
    try
    {
        // Do some stuff with it
    }
    catch (JMSException e)
    {
        e.printStackTrace();
    }
}

发件人:

@Inject
private JMSContext context;
@Resource(lookup = "java:/jms/topic/refresh")
private Destination topic;

MapMessage mesg = context.createMapMessage();
// set message body
context.createProducer().send(topic, mesg);

发送完一条消息后,尽管设置了 AUTO_ACKNOWLEDGE,客户端仍会收到大量消息。

我怎样才能减慢发件人的速度?

如果需要SSCCE,我可以提供一个,包含(服务器,客户端,配置等)很多

经纪人正在做它的工作,即尽快将消息传递给您的客户。如果您的客户无法处理,那么还有其他工具(如 Apache Camel)提供 Throttler type components which you can use to insert in between the client and broker to slow down the incoming messages. ActiveMQ supports embedded Camel routes,因此您可以在代理上进行设置。

如果您想最大程度地控制应用程序中处理消息的速率,那么您应该切换到同步消费者模型并仅在您准备好处理新消息时才在消费者上调用接收。