我在JMS上运行测试用例,发现处理是顺序的。当我向一个使用JMS发送消息的servlet发出200个请求时,接收者(messageListner)正在按顺序恢复请求。如何接收并发请求?我们要设置任何参数吗?我阅读了JMS教程和API,它们在同一个会话中按顺序传递消息,即使我正在为每个发送请求创建一个新会话&接收端的10个会话仍然是顺序处理。
public class ProducerServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JMS context factory.
public final static String JMS_FACTORY = "jms/TestConnectionFactory";
// Defines the queue.
public final static String QUEUE = "jms/TestJMSQueue";
public final static String TOPIC = "jms/TestTopic";
TestJMSListener jms = new TestJMSListener();
ConnectionFactory connectionFactory = null;
Queue dest1 = null;
Topic dest =null;
Connection connection = null;
MessageProducer producer = null;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(dest1);
TextMessage message = session.createTextMessage();
message.setText("This is message from JMSSECOND DEMO "
+ request.getParameter("Num"));
System.out.println("Sending message: " + message.getText());
producer.send(message);
producer.send(session.createMessage());
} catch (Exception e) {
System.out.println("Exception occurred: " + e.toString());
}
}
@Override
public void init(ServletConfig arg0) throws ServletException {
Context jndiContext = null;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, "http://localhost:7001");
jndiContext = new InitialContext(env);
} catch (NamingException e) {
System.out.println("Could not create JNDI API context: "
+ e.toString());
}
try {
connectionFactory = (ConnectionFactory) jndiContext
.lookup(JMS_FACTORY);
dest1 = (Queue) jndiContext.lookup(QUEUE);
} catch (Exception e) {
System.out.println("JNDI API lookup failed: " + e.toString());
e.printStackTrace();
}
}
}
Listner实现,在收到消息后我将要睡觉(做一些事情)。
public class TestJMSListener implements MessageListener {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JMS context factory.
public final static String JMS_FACTORY = "jms/TestConnectionFactory";
// Defines the queue.
public final static String QUEUE = "jms/TestJMSQueue";
public final static String TOPIC = "jms/TestTopic";
public TestJMSListener() {
System.out.println("********* Consumer check **********");
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection[] = null;
Session session[] = null;
Queue dest1 = null;
Topic dest = null;
MessageConsumer consumer[] = null;
// TextMessage message = null;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, "http://localhost:7001");
jndiContext = new InitialContext(env);
} catch (NamingException e) {
System.out.println("Could not create JNDI API context: "
+ e.toString());
System.exit(1);
}
try {
connectionFactory = (ConnectionFactory) jndiContext
.lookup(JMS_FACTORY);
dest1 = (Queue) jndiContext.lookup(QUEUE);
} catch (Exception e) {
System.out.println("JNDI API lookup failed: " + e.toString());
System.exit(1);
}
connection = new Connection[10];
session = new Session[10];
consumer = new MessageConsumer[10];
for (int i = 0; i < 10; i++) {
try {
connection[i] = connectionFactory.createConnection();
session[i] = connection[i].createSession(false,
Session.AUTO_ACKNOWLEDGE);
consumer[i] = session[i].createConsumer(dest);
consumer[i].setMessageListener(this);
connection[i].start();
} catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
}
}
}
@Override
public void onMessage(Message m) {
if (m instanceof TextMessage) {
TextMessage message = (TextMessage) m;
try {
System.out.println("Reading message from Listener: "
+ new Date() + message.getText());
Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我正在使用Weblogic 11g,其默认配置为ConnectionFactory&amp;队列。当我使用Topic时,它实际上每秒只传送一条消息(即在完成第一条消息之后),而对于Queue,它每秒传递2到3条消息。如何让我的监听器支持并发处理。
添加了更多的侦听器对象,它们在列表中找到了多个会话/使用者,从而解决了目的。 在下面找到修改后的代码。
public class ProducerServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JMS context factory.
public final static String JMS_FACTORY = "jms/TestConnectionFactory";
// Defines the queue.
public final static String QUEUE = "jms/TestJMSQueue";
public final static String TOPIC = "jms/TestTopic";
TestJMSListener listeners[] = new TestJMSListener[20];
ConnectionFactory connectionFactory = null;
Queue dest1 = null;
Topic dest =null;
Connection connection = null;
MessageProducer producer = null;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(dest1);
TextMessage message = session.createTextMessage();
message.setText("This is message from JMSSECOND DEMO "
+ request.getParameter("Num"));
System.out.println("Sending message: " + message.getText());
producer.send(message);
producer.send(session.createMessage());
} catch (Exception e) {
System.out.println("Exception occurred: " + e.toString());
}
}
@Override
public void init(ServletConfig arg0) throws ServletException {
Context jndiContext = null;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, "http://localhost:7001");
jndiContext = new InitialContext(env);
} catch (NamingException e) {
System.out.println("Could not create JNDI API context: "
+ e.toString());
}
try {
connectionFactory = (ConnectionFactory) jndiContext
.lookup(JMS_FACTORY);
dest1 = (Queue) jndiContext.lookup(QUEUE);
for(int i=0;i<listeners.length;i++ ){
listeners[i]=new TestJMSListener(Integer.toString(i+1));
}
} catch (Exception e) {
System.out.println("JNDI API lookup failed: " + e.toString());
e.printStackTrace();
}
}
}
public class TestJMSListener implements MessageListener {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JMS context factory.
public final static String JMS_FACTORY = "jms/TestConnectionFactory";
// Defines the queue.
public final static String QUEUE = "jms/TestJMSQueue";
public final static String TOPIC = "jms/TestTopic";
public String listnerNum = "";
public TestJMSListener(String listerNo) {
super();
System.out.println("********* Consumer check **********");
listnerNum = listerNo;
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Queue dest1 = null;
Topic dest = null;
MessageConsumer consumer = null;
// TextMessage message = null;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, "http://localhost:7001");
jndiContext = new InitialContext(env);
} catch (NamingException e) {
System.out.println("Could not create JNDI API context: "
+ e.toString());
System.exit(1);
}
try {
connectionFactory = (ConnectionFactory) jndiContext
.lookup(JMS_FACTORY);
dest1 = (Queue) jndiContext.lookup(QUEUE);
} catch (Exception e) {
System.out.println("JNDI API lookup failed: " + e.toString());
System.exit(1);
}
try{
connection = connectionFactory.createConnection();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(dest1);
consumer.setMessageListener(this);
connection.start();
} catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
}
}
@Override
public void onMessage(Message m) {
if (m instanceof TextMessage) {
TextMessage message = (TextMessage) m;
try {
System.out.println("Reading message from Listener: "+listnerNum+ " : "
+ new Date() + message.getText());
Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
答案 0 :(得分:3)
我看了你的解决方案并意识到你有几个并发会话,消费者等,但是有一个队列来处理所有这些。队列是一个队列,所有队列都顺序通过管道。如果您碰巧只有其中一个 - 那么您在该点上有一个执行线程,所有内容都是顺序的,因为队列不会让并发事件发生。
如果在不同的线程中实现several queues,您的计算机可以同时处理多个调用。几个队列可能意味着使用不同的队列名称等,但为了这个麻烦,您可以使用负载均衡器解决方案,如Apache Camel来实际为您选择队列。至少this closed post让我理解这种队列和线程的组合是可能的。
然后,平衡器为每个请求选择单独的队列,每个队列使其自己执行顺序工作来处理请求。然后,并发会话的数量就是配置问题。
答案 1 :(得分:3)
在你的代码中,你只有一个监听器实例(创建Servlet实例时创建),因此你只会接收序列消息,无论你有多少个发送者会话......它都是Just Queue。
如果您希望同时接收,那么您可能需要多个Listener,并且只有一个时间消息将在任何一个侦听器中传递。
如果要同时处理消息,则一旦顺序传递,然后创建线程池并在单独的线程中对进程进行处理并返回到侦听模式。
注意**在此模式下,您可能无法正确处理Ack模式,因为您在没有完成消息过程的情况下确认。
答案 2 :(得分:0)
我不是WebLogic JMS的专家,但是你的代码看起来还不错(除了你正在创建的多个连接,这不是必需的,多个会话应该就足够了。我甚至认为多个连接可能会产生负面影响,因为它们消耗线程),它应该同时使用。既然你说使用Queue,你每秒收到2-3条消息,你实际上会同时得到它们(因为每个听众都会睡一秒钟)。
因为你说你得到每秒2-3次,我认为你实际上得到4-6,因为每个第二条消息都没有打印(因为它不是TextMessage),因为生产者发送了一个TextMessage('producer.send (message)'和一个空的'producer.send(session.createMessage())')。 除此之外,我将检查ConnectionFactpry的服务器配置。我记得有人需要为MDB配置WorkManager线程,但遗憾的是不确定“手动”JMS客户端。
所以我这样做:
希望有所帮助。
此致
梅西