如何从weblogic JMS队列中清除/删除邮件
您可以使用JMX从Java或WLST(Python)中清除队列。您可以在http://download.oracle.com/docs/cd/E11035_01/wls100/wlsmbeanref/core/index.html上找到WLS 10.0的MBean定义。 这是一个基本的Java例如(不要忘记把weblogic.jar中的CLASSPATH):
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.ObjectName;
import javax.naming.Context;
import weblogic.management.mbeanservers.runtime.RuntimeServiceMBean;
public class PurgeWLSQueue {
private static final String WLS_USERNAME = "weblogic";
private static final String WLS_PASSWORD = "weblogic";
private static final String WLS_HOST = "localhost";
private static final int WLS_PORT = 7001;
private static final String JMS_SERVER = "wlsbJMSServer";
private static final String JMS_DESTINATION = "test.q";
private static JMXConnector getMBeanServerConnector(String jndiName) throws Exception {
Hashtable<String,String> h = new Hashtable<String,String>();
JMXServiceURL serviceURL = new JMXServiceURL("t3", WLS_HOST, WLS_PORT, jndiName);
h.put(Context.SECURITY_PRINCIPAL, WLS_USERNAME);
h.put(Context.SECURITY_CREDENTIALS, WLS_PASSWORD);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
return connector;
}
public static void main(String[] args) {
try {
JMXConnector connector =
getMBeanServerConnector("/jndi/"+RuntimeServiceMBean.MBEANSERVER_JNDI_NAME);
MBeanServerConnection mbeanServerConnection =
connector.getMBeanServerConnection();
ObjectName service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
ObjectName serverRuntime = (ObjectName) mbeanServerConnection.getAttribute(service, "ServerRuntime");
ObjectName jmsRuntime = (ObjectName) mbeanServerConnection.getAttribute(serverRuntime, "JMSRuntime");
ObjectName[] jmsServers = (ObjectName[]) mbeanServerConnection.getAttribute(jmsRuntime, "JMSServers");
for (ObjectName jmsServer: jmsServers) {
if (JMS_SERVER.equals(jmsServer.getKeyProperty("Name"))) {
ObjectName[] destinations = (ObjectName[]) mbeanServerConnection.getAttribute(jmsServer, "Destinations");
for (ObjectName destination: destinations) {
if (destination.getKeyProperty("Name").endsWith("!"+JMS_DESTINATION)) {
Object o = mbeanServerConnection.invoke(
destination,
"deleteMessages",
new Object[] {""}, // selector expression
new String[] {"java.lang.String"});
System.out.println("Result: "+o);
break;
}
}
break;
}
}
connector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
在单节点环境中工作的伟大,但如果你是一个集群环境中的一个迁移会发生什么JMSServer(当前在节点#1上)并且此代码在节点#2上执行。那么将不会有JMSServer可用,并且不会删除任何消息。
这是我面对现在的问题...
是否有连接到JMSQueue而无需JMSServer可用的方法吗?
[编辑]
找到了解决方案:使用域运行时服务,而不是:
ObjectName service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
,并确保访问WLS-群集上管理端口。
这里是WLST一个例子在端口7005上运行的托管服务器:
connect('weblogic', 'weblogic', 't3://localhost:7005')
serverRuntime()
cd('/JMSRuntime/ManagedSrv1.jms/JMSServers/MyAppJMSServer/Destinations/MyAppJMSModule!QueueNameToClear')
cmo.deleteMessages('')
最后的命令应该返回它删除的邮件数量。
,如果这是一个时间,最简单的是通过控制台来做到这一点...
在以下链接程序可以帮助您根据重新传递消息参数仅清除未决从队列中的消息
http://techytalks.blogspot.in/2016/02/deletepurge-pending-messages-from-jms.html
由于这是你的第一个答案。因此我没有说任何话,但尽量避免外部链接,并显示你的一些努力。 – surajsn 2016-02-19 07:18:17
我试过,但得到这个异常:'java.lang.UnsupportedOperationException:deleteMessages(字符串)无效类weblogic.jms.backend.BEDestinationRuntimeMBeanImpl'即使'LS()''包括-rx deleteMessages整数:字符串(选择器)'在它返回的信息中。 – pharsicle 2013-05-01 00:22:09
修复了我自己!我的情况有一个持久订户的话题。我需要为用户切换到MBean,并在其上调用deleteMessages('')'。 – pharsicle 2013-05-01 00:56:58