To delete durable subscription we need to use unsubscribe method of the session. Here is simple utility that can be used to achieve just that (written for Tibco EMS).
First - let's create a class that will hold connection parameters. We will try to make this class generic enough, to be useful for other utilities that use MQ or EMS.
/***********************************************************
** UtilJMSConnectionParameters
************************************************************/
package utilities;
public class UtilJMSConnectionParameters {
private String Host;
private String Port;
private String Queue;
private String Topic;
private String Durable;
private String Channel;
private String Queuemanager;
private String User;
private String Password;
private String Commit;
private String Filename;
/*
* Getters, Setters
*/
public String getFilename() {
return Filename;
}
public void setFilename(String filename) {
Filename = filename;
}
/*
** ..... Add more Getters/Setters
*/
/**
* isEmptyUser
*/
public boolean isEmptyUser(){
return isEmptyField(this.User);
}
/**
* isEmptyPassword
*/
public boolean isEmptyPassword(){
return isEmptyField(this.Password);
}
public boolean isEmptyHost(){
return isEmptyField(this.Host);
}
public boolean isEmptyDurable(){
return isEmptyField(this.Durable);
}
public boolean isEmptyQueue(){
return isEmptyField(this.Queue);
}
public boolean isEmptyTopic(){
return isEmptyField(this.Topic);
}
public boolean toCommit(){
boolean retEmpty = true;
if(this.Commit == null){
retEmpty = false;
}
return retEmpty;
}
/**
*
* @param field
* @return
*/
private boolean isEmptyField(String field){
boolean retEmpty = false;
if(field == null){
retEmpty = true;
}
else if (field.trim().length() == 0){
retEmpty = true;
}
return retEmpty;
}
/**
* toString
*/
public String toString(){
StringBuffer str = new StringBuffer();
str.append(" host: "+this.Host);
str.append("\n port: "+this.Port);
str.append("\n channel: "+this.Channel);
str.append("\n queuemanager: "+this.Queuemanager);
str.append("\n topic: "+this.Topic);
str.append("\n durable: "+this.Durable);
str.append("\n queue: "+this.Queue);
str.append("\n user: "+this.User);
str.append("\n password: "+this.Password);
if(this.toCommit()){
str.append("\n Commit requested");
}
return str.toString();
}
}
/************************************************
** End of UtilJMSConnectionParameters definition
*************************************************/
|
Now - the main class
We are using CmdLineParameterReader class described in the previous post to get parameters from command line.
/**********************************************************
** UtilJMSTibUnsubscribe
***********************************************************/
package utilities;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Session;
public class UtilJMSTibUnsubscribe implements ExceptionListener {
private static UtilJMSConnectionParameters connParams =
new UtilJMSConnectionParameters();
/**
* UtilJMSMQListen
*
* @param argc
*/
private UtilJMSTibUnsubscribe(String[] argc){
CmdLineParameterReader cmdReader = new CmdLineParameterReader("-");
connParams.setHost(cmdReader.getParameterValue(argc,"-host"));
connParams.setPort(cmdReader.getParameterValue(argc,"-port"));
connParams.setDurable(cmdReader.getParameterValue(argc,"-durable"));
connParams.setUser(cmdReader.getParameterValue(argc,"-user"));
connParams.setPassword(cmdReader.getParameterValue(argc,"-pass"));
if(connParams.isEmptyHost() || connParams.isEmptyDurable()){
System.out.println("Provide parameters: -host [hostname] "+
"-port [port] -durable [durable] " +
"-user [user] -pass [password]\n " +
" need at least host and durable name \n");
System.exit(1);
}
}
/**
* MQListen
*
* @param connParams
*/
private void TibListen(UtilJMSConnectionParameters connParams){
System.out.println("Connection arameters\n"+
connParams.toString()+"\n");
Connection connection = null;
Session session = null;
try{
/* Create a connection factory */
ConnectionFactory factory =
new com.tibco.tibjms.TibjmsConnectionFactory(
"tcp://"+connParams.getHost()+":"+connParams.getPort()
);
/* Create a connection */
connection = factory.createConnection(connParams.getUser(),
connParams.getPassword());
/* Create Sessioin */
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
/* set the exception listener */
connection.setExceptionListener(this);
/* unsubscribe */
session.unsubscribe(connParams.getDurable());
System.out.println("Unsubscribed "+ connParams.getDurable());
connection.close();
System.out.println("Connection closed, exiting ");
System.exit(0);
}
catch(Exception ex){
System.out.println("Exception: "+ex.toString());
ex.printStackTrace();
}
}
/**
* onException
*/
public void onException(JMSException ex) {
System.err.println("Connection exception received");
System.out.println("Connection exception "+ex.toString());
ex.printStackTrace();
System.exit(-1);
}
/**
*
* @param argc
*/
public static void main(String[] argc){
UtilJMSTibUnsubscribe listener = new UtilJMSTibUnsubscribe(argc);
listener.TibListen(connParams);
}
}
/**************************************************
** End of UtilJMSTibUnsubscribe definition
***************************************************/
|
Compile this utility and jar it into UtilJms.jar
This utility can be used from a shell script that will look something like below.
#!/bin/bash
JAVA_HOME=/bd/apps/java/jrrt-3.1.0-1.5.0
export JAVA_HOME
if [ $# -lt 4 ]; then
echo "Usage:JMSUnsubscribe.sh [host] [port] [durable] [user] [pass]"
exit ;
fi
HOST=$1
PORT=$2
DURABLE=$3
USER=$4
PASSWORD=$5
ClassPath="";
ClassPath=${ClassPath}"./lib/UtilJms.jar:"
ClassPath=${ClassPath}"./lib/jms.jar:"
ClassPath=${ClassPath}"./lib/tibjms.jar:"
export ClassPath
echo "ClassPath:${ClassPath}"
$JAVA_HOME/bin/java -classpath "${ClassPath}" utilities.UtilJMSTibUnsubscribe \
-host ${HOST} -port ${PORT} -durable ${DURABLE} -user ${USER} -pass ${PASSWORD}
|
NOTE:
Be careful - if you have any messages piled up, unsubscribing will cause the server to drop them.
No comments:
Post a Comment