Java 如何有效地处理JMS队列以避免服务器负载
这里我的问题是,如果jms队列(Wildfly 10)在很大程度上被填满,我如何处理服务器上的负载。 问题是消息不断地添加到JMS队列中,但它们是逐个处理的。编写的逻辑只是逐个处理消息。我们不能一次处理多个消息,因为逻辑无法更改。但这会增加服务器上的负载,因为队列中装载了许多消息。 此时如何管理队列 以下是我用于生产和消费的示例代码:- 下面的代码每100毫秒运行一次schedular,从数据库中获取100条消息并发送到队列 Arraylist arrMessages=GetMessagesFromdatabase() 对于(messageobjectobj:arrMessages)sendMessagetoQueue(obj) 生产商代码:-Java 如何有效地处理JMS队列以避免服务器负载,java,wildfly-10,jms-queue,Java,Wildfly 10,Jms Queue,这里我的问题是,如果jms队列(Wildfly 10)在很大程度上被填满,我如何处理服务器上的负载。 问题是消息不断地添加到JMS队列中,但它们是逐个处理的。编写的逻辑只是逐个处理消息。我们不能一次处理多个消息,因为逻辑无法更改。但这会增加服务器上的负载,因为队列中装载了许多消息。 此时如何管理队列 以下是我用于生产和消费的示例代码:- 下面的代码每100毫秒运行一次schedular,从数据库中获取100条消息并发送到队列 Arraylist arrMessages=GetMessagesFr
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());
}
不要使用内置的JMS服务器,而是独立安装。将其配置为溢出到磁盘,并确保它可以处理积压工作。这样,当队列已满时,wildfly的性能将不会受到影响。这也将允许您扩展,如果队列与您的应用程序位于同一个应用程序服务器中,则很难做到这一点。但是如何实现这一点,因为我们的队列已添加到作为应用程序服务器的wildfly服务器是的,因此如果您不希望代理在wildfly内部运行,则需要使用另一个队列。居住在独立经纪人中的人。需要将客户端和服务器都指向该代理。或者,可能有一些配置嵌入式代理以更好地处理内存压力的选项,但我没有使用它们。