【发布时间】:2017-01-30 19:18:53
【问题描述】:
我使用 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,我可以提供一个,包括很多内容(服务器、客户端、配置等)
【问题讨论】: