本文介绍了Web应用服务器中的setMessageListener jms方法生成javax.jms.IllegalStateException:方法setMessageListener不允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用Websphere Application Server 7.x版本。I am using Websphere Application Server 7.x version.我想在JMS API的帮助下阅读MQ中缓冲的消息,如下所示。 我是从 link1 中提到的, link2 I want to read the messages buffered in MQ with the help of JMS API as given below.I referred from link1, link2 @Resource(lookup = "jms/ConnectionFactory") private static ConnectionFactory connectionFactory; @Resource(lookup = "jms/Queue") private static Queue queue; public void readMessagesFromQueue() { try { Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queue); MyListener myListener = new MyListener(); consumer.setMessageListener(myListener); // Error here connection.start(); } catch (JMSException e) { throw new RuntimeException(e); } } MyListener class:public class MyListener implements MessageListener { @Override public void onMessage(Message message) { try { String text=((TextMessage)message).getText(); System.out.println(text); } catch (JMSException e) { throw new RuntimeException(e); } }} 在运行期间我得到异常的时间:javax.jms.IllegalStateException: Method setMessageListener not permitted我可以在链接表明方法 setMessageListener 用于异步消息传递,禁止在WebSphere Application Server中使用此方法。I could see in the link that the method setMessageListener for asynchronous messaging, it is forbidden to use this method in a WebSphere Application Server. 问题1: 这不能 setMessageListener 在WAS中使用的方法? 如果没有,实现上述所需功能的工作是什么?Can't this setMessageListener method be used in WAS?If not, what is the work around to achieve the above required functionality? 问题2: 我可以用其他话来解释这个问题: JMS列表器不应该回复消息尽快消息到达队列中。但我希望听众只在我需要的时候听。 我尝试过上面提到的方法。但是由于异常我被阻止了。I can explain the need in other words:The JMS listner should not recive the messages as soon as the message arrived in the the queue. But I want the listener to listen only during the time I require. I tried the approach which I mentioned above. But I got blocked because of the exception.任何其他方法来实现这个目标吗?Any other way to achieve this?推荐答案如果要停止接收消息,可以取消激活管理MDB的激活规范。您可以编写wsadmin脚本并将其放在crontab中,通过管理控制台暂停,或使用JMX和MBean暂停。If you want to stop receiving messages you can deactivate activation specification that manages the MDB. You can either write wsadmin script and put it for example in the crontab, pause it via admin console, or use JMX and MBean to pause.检查以下链接: 使用wsadmin脚本编制管理消息端点生命周期 示例1:处理计划中断的MDB或SCA应用程序外部资源 AdminService API MBean列表 Managing the message endpoint lifecycle using wsadmin scriptingExample 1: Handling a planned outage of an MDB or SCA application external resourceAdminService APIList of MBeans 更新 以下是代码示例。这是伪代码 - 没有错误处理,没有检查它是否正确ActiveSpec。 此代码必须以角色管理员或操作员的用户身份运行(因此需要启用应用程序安全性),我是通过为servlet指定 @RunAs 角色来完成的它调用了它,但您可能需要以不同的方式执行它。Here is code sample. This is pseudo code - no error handling, no checking if it is correct ActiveSpec.This code must be run as user with role administrator or operator (so application security needs to be enabled), I did it by specifying @RunAs role for the servlet which invoked it, but you may need to do it differently.protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String action = request.getParameter("action"); try { AdminService adminService = AdminServiceFactory.getAdminService(); ObjectName asName = new ObjectName("WebSphere:*,type=J2CMessageEndpoint"); Set names = adminService.queryNames(asName, null); System.out.println("Found:" + names.size()); if(names.size() > 0) { ObjectName mbean = (ObjectName) names.iterator().next(); System.out.println("mbean: " + mbean); if(action != null && action.equals("pause")) { adminService.invoke((ObjectName) mbean, "pause", null, null); System.out.println("invoked"); } else if(action != null && action.equals("resume")) { adminService.invoke((ObjectName) mbean, "resume", null, null); System.out.println("invoked"); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }}您应该在日志中看到AS已停用且已激活:You should see in the log that AS is deactivated and activated:[4/20/17 4:24:41:029 PDT] 00000094 SystemOut O mbean: WebSphere:name=JMXTestEAR#JMXTestWeb.war#TestMDB_J2CMessageEndpoint,process=server1,ActivationSpec=jms/MyAS,platform=dynamicproxy,node=ubuntuNode01,J2EEApplication=JMXTestEAR,j2eeType=JCAMessageEndpoint,J2EEServer=server1,Server=server1,version=9.0.0.3,J2CResourceAdapter=SIB JMS Resource Adapter,type=J2CMessageEndpoint,mbeanIdentifier=cells/ubuntuNode01Cell/nodes/ubuntuNode01/servers/server1/resources.xml#J2CResourceAdapter_1492682844859#JMXTestEAR#JMXTestWeb.war#TestMDB_J2CMessageEndpoint,cell=ubuntuNode01Cell,MessageDrivenBean=JMXTestEAR#JMXTestWeb.war#TestMDB,spec=1.0[4/20/17 4:24:41:037 PDT] 00000094 ActivationSpe I J2CA0524I: The Message Endpoint for ActivationSpec jms/MyAS (com.ibm.ws.sib.api.jmsra.impl.JmsJcaActivationSpecImpl) and MDB Application JMXTestEAR#JMXTestWeb.war#TestMDB is deactivated.[4/20/17 4:24:41:038 PDT] 00000094 SystemOut O invoked[4/20/17 4:24:52:890 PDT] 00000094 SystemOut O Found:1[4/20/17 4:24:52:890 PDT] 00000094 SystemOut O mbean: WebSphere:name=JMXTestEAR#JMXTestWeb.war#TestMDB_J2CMessageEndpoint,process=server1,ActivationSpec=jms/MyAS,platform=dynamicproxy,node=ubuntuNode01,J2EEApplication=JMXTestEAR,j2eeType=JCAMessageEndpoint,J2EEServer=server1,Server=server1,version=9.0.0.3,J2CResourceAdapter=SIB JMS Resource Adapter,type=J2CMessageEndpoint,mbeanIdentifier=cells/ubuntuNode01Cell/nodes/ubuntuNode01/servers/server1/resources.xml#J2CResourceAdapter_1492682844859#JMXTestEAR#JMXTestWeb.war#TestMDB_J2CMessageEndpoint,cell=ubuntuNode01Cell,MessageDrivenBean=JMXTestEAR#JMXTestWeb.war#TestMDB,spec=1.0[4/20/17 4:24:52:897 PDT] 00000094 SibMessage I [:] CWSIV0777I: A connection to messaging engine ubuntuNode01.server1-myBus for destination testQ1 on bus myBus has been successfully created.[4/20/17 4:24:52:898 PDT] 00000094 ActivationSpe I J2CA0523I: The Message Endpoint for ActivationSpec jms/MyAS (com.ibm.ws.sib.api.jmsra.impl.JmsJcaActivationSpecImpl) and MDB Application JMXTestEAR#JMXTestWeb.war#TestMDB is activated.[4/20/17 4:24:52:899 PDT] 00000094 SystemOut O invoked 这篇关于Web应用服务器中的setMessageListener jms方法生成javax.jms.IllegalStateException:方法setMessageListener不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 20:38