日期:2014-05-20  浏览次数:20700 次

一个weblogic的jms问题
各位大虾:
  小弟使用weblogic作为中间件发送jms消息时遇到一个问题就是,当重新启动接收端容器的时候。容器会接收一些已经接收过的消息。所以小弟想请问一下各位大虾,怎样在收到jms消息加以处理以后,将这个消息清除。我的发送端代码和客户端代码如下:
  下面是发送端代码:
  public class JmsCommons {

private QueueConnectionFactory qconFactory;

private QueueConnection qcon;

private QueueSession qsession;

private QueueSender qsender;

private Queue queue;

//4个参数: 
public JmsCommons(String paramString1, String paramString2,
String paramString3, String paramString4) {
try {
Hashtable localHashtable = new Hashtable();
localHashtable.put("java.naming.factory.initial", paramString2);
localHashtable.put("java.naming.provider.url", paramString1);
InitialContext localInitialContext = new InitialContext(
localHashtable);
this.qconFactory = ((QueueConnectionFactory) localInitialContext
.lookup(paramString3));
this.qcon = this.qconFactory.createQueueConnection();
this.qsession = this.qcon.createQueueSession(false, 1);
this.queue = ((Queue) localInitialContext.lookup(paramString4));
this.qsender = this.qsession.createSender(this.queue);

} catch (Exception localException) {
localException.printStackTrace();
}
}

public void sendMessage(String paramString) {
try {
this.qcon.start();
TextMessage localTextMessage = this.qsession
.createTextMessage(paramString);
this.qsender.send(localTextMessage);
this.qsender.close();
} catch (Exception localException) {
localException.printStackTrace();
}
}
下面是接收端的一个监听线程
public class ReceiveJmsMessage implements Runnable {
 
 
private String jndi_factory;
private String jms_factory;
private String jms_queue;
private String url;
private MessageConsumer comsumer;
private TSjjhDsrwDao rwDao;
private TSjjhXxrzDao xxDao;
 
 
public ReceiveJmsMessage(){}
 
public ReceiveJmsMessage(String jndi_factory, String jms_factory,
String jms_queue,String url,TSjjhDsrwDao rwDao,TSjjhXxrzDao xxDao) {
this.jndi_factory = jndi_factory;
this.jms_factory = jms_factory;
this.jms_queue = jms_queue;
this.url=url;
this.rwDao=rwDao;
this.xxDao=xxDao;
}
 
 

public void receive() {
Hashtable<String,String> env=new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,jndi_factory);
env.put(Context.PROVIDER_URL,url);
try {
Context context=new InitialContext(env);
Queue queue=null;
queue=(Queue) context.lookup(jms_queue);
QueueConnectionFactory factory=null;
factory=(QueueConnectionFactory) context.lookup(jms_factory);
if (factory!=null&&queue!=null) {
QueueConnection queueconnection = null;
queueconnection = factory.createQueueConnection();
if(queueconnection!=null){
queueconnection.start();
QueueSession queuesession = null;
queuesession = (WLQueueSession)queueconnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
if (queuesession!=null) {
comsumer = queuesession.createConsumer(queue);
}else{
System.out.println("JMS 监听接收失败");
}
}else{
System.out.println("JMS 监听接收失败");
}
}else{
System.out.println("JMS 监听接收失败");
}
} catch (Exception e) {
e.printStackTrace();
}

}

public void run() {
try {
receive();
while (true) {

Object xmlmessage = comsumer.receive(1000);

if (xmlmessage!=null&&(xmlmessage instanceof TextMessage)) {
  //相关处理逻辑