I use Spring + Hibernate + JPA in my application.
I need to read the message from Websphere MQ
and insert the message to DB.
Sometimes there may be continuous messages available and sometimes very less number of messages and sometimes we can expect no message from Queue
.
Currently I'm reading the message one by one and inserting them to Database. But it does not help much in terms of performance.
I mean when I have chunk of messages(Example 300k messages in Queue) I could not insert them faster. Number of entities inserted to DB per second is not so high. Because I do commit for every single entity.
I want to use hibernate batch processing, so that I can insert list of entities in a single commit. (Example: 30 to 40 messages per commit)
Questions:
How to receive multiple messages from Queue? (I have checked that BatchMessageListenerContainer may be helpful. But I could not get some reference)
Should I separate the db insertion process out side onMessage method? So that thread will be released to pool and be available for picking next messages from Queue?
Parallel threads usage?
Current implementation:
Message Listener:
<bean id="myMessageListener" class="org.mypackage.MyMessageListener">
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destinationName" ref="queue"/>
<property name="messageListener" ref="myMessageListener"/>
<property name ="concurrentConsumers" value ="10"/>
<property name ="maxConcurrentConsumers" value ="50"/>
</bean>
Listener Class:
package org.mypackage.MyMessageListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import org.mypackage.service.MyService;
public class MyMessageListener implements MessageListener {
@Autowired
private MyService myService;
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
// parse the message
// Process the message to DB
} catch (JMSException e1) {
e1.printStackTrace();
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…