我的问题是要知道,如果jms队列(Wildfly 10)被高度填充,我该如何处理服务器上的负载。 问题是消息不断被添加到JMS队列中,但它们是一一处理的。编写的逻辑是仅一一处理消息。我们无法一次处理多个消息,因为逻辑无法更改。但这会导致服务器上的负载增加,因为队列中加载了许多消息。 此时我该如何管理队列。
这是我用于生产和消费的示例代码:-
以下代码每隔100毫秒运行一次计划,并从数据库中提取100条消息并发送到队列。
Arraylist arrMessages = GetMessagesFromdatabase();
for(MessageObject obj:arrMessages)sendMessagetoQueue(obj);
生产者代码:-
void sendMessagetoQueue(MessageObject messageObject){
ConnectionFactory connectionFactory = JMSConstants.getConnFactory.getJmsConnectionFactory();
Context initialContext = JMSConstants.getConnFactory.getInitialcontext();
Connection connection = null;
String destinationName = "java:/jms/queue/Queue";
MessageProducer publisher = null;
Session session = null;
try {
connection = connectionFactory.createConnection();
Queue queue = (Queue) initialContext
.lookup(destinationName);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
publisher = session.createProducer(queue);
connection.start();
ObjectMessage objMessage = session.createObjectMessage(messageObject);
publisher.send(objMessage);
} catch (Exception exc) {
exc.printStackTrace();
LOG.error(exc);
}
}
然后另一个时间表每100毫秒运行一次,以消耗队列中的消息并将其转发以进行逐一处理。
消费者代码:-
ConnectionFactory connectionFactory = JMSConstants.getConnFactory.getJmsConnectionFactory();
Context initialContext = JMSConstants.getConnFactory.getInitialcontext();
Connection connection = null;
String destinationName = "java:/jms/queue/Queue";
Session session = null;
MessageConsumer consumer = null;
Boolean checkFlag = true;
QueueBrowser queueBrowserconnect = null;
try {
connection = connectionFactory.createConnection();
Queue queue = (Queue) initialContext.lookup(destinationName);
session = connection.createSession();
consumer = session.createConsumer(queue);
Queue senderqueueconnect = (Queue) initialContext.lookup(senderQueueconnect);
queueBrowserconnect = session.createBrowser(senderqueueconnect);
connection.start();
ObjectMessage objectMessage = (ObjectMessage) consumer.receive(1);
if (objectMessage != null) {
objectMessage.acknowledge();
MessageObject messageobject= (MessageObject) objectMessage.getObject();
//Send object for processing
messageService.processInputFromQueue(messageobject);
}
} catch (Exception exc) {
exc.printStackTrace();
LOG.error(exc.getMessage());
}