/*
* jmsFront.java
*
* Created on 2003年7月19日, 上午11:26
*/
package collector.jms;
import java.net.*;
import java.util.*;
import javax.jms.*;
import javax.naming.*;
import collector.common.*;
import collector.common.queueobject.*;
import pbs.service.struct.*;
/**
*
* @author WangJun
*/
public class JmsFront
implements MessageListener {
private Context m_InitialContext = null;
private TopicConnectionFactory m_TopicConnectionFactory = null;
private Topic m_Topic = null;
private TopicSession m_TopicSession = null;
private TopicConnection m_TopicConnection = null;
private TopicPublisher m_TopicPublisher = null;
private TopicSubscriber m_TopicSubscriber = null;
private ArrayList m_NotifyQueue = null;
private ArrayList m_CollectorQueue = null;
private String m_LocalAddress = "";
private int m_SendTimes = 0;
private int m_RecTimes = 0;
private String m_AppserverType = " ";
/** Creates a new instance of jmsFront */
public JmsFront(String m_LocalAddrsss) {
this.m_NotifyQueue = new ArrayList();
this.m_CollectorQueue = new ArrayList();
this.m_LocalAddress = getLocalHostName();
}
private String getLocalHostName() {
String m_TempLocalName = "";
try {
InetAddress ia = InetAddress.getLocalHost();
m_TempLocalName = ia.getHostName();
}
catch (Exception e) {
CollectorDefine.SystemPrintln("getLocalHostName In JmsComm Error #1" +
e.toString());
return "##";
}
return m_TempLocalName;
}
public int getNotifyQueueNum() {
if (this.m_NotifyQueue.isEmpty() == true) {
return -1;
}
return this.m_NotifyQueue.size();
}
private int putNotifyQueue(Object m_Object) {
this.m_NotifyQueue.add(m_Object);
return 1;
}
public Object getNotifyQueue() {
// wj modi at hangzhou 20040602
//return (Object)this.m_NotifyQueue.remove(0);
if (this.m_NotifyQueue.size() > 0) {
return (Object)this.m_NotifyQueue.remove(0);
}
return null;
// wj modi at hangzhou 20040602
}
public int removeAllNotifyQueue() {
// this.m_NotifyQueue.clear();
if (CollectorDefine.APPSERVER_CLUSTER == 1) {
// if (m_AppserverType.toLowerCase().equals("ibm") != true) {
this.m_NotifyQueue.clear();
}
return 1;
}
public int getCollectorQueueNum() {
if (this.m_CollectorQueue.isEmpty() == true) {
return -1;
}
return this.m_CollectorQueue.size();
}
private int putCollectorQueue(Object m_Object) {
this.m_CollectorQueue.add(m_Object);
return 1;
}
public Object getCollectorQueue() {
// wj modi at hangzhou 20040602
//return (Object)this.m_CollectorQueue.remove(0);
if (this.m_CollectorQueue.size() > 0) {
return (Object)this.m_CollectorQueue.remove(0);
}
return null;
// wj modi at hangzhou 20040602
}
public int removeAllCollectorQueue() {
// this.m_CollectorQueue.clear();
if (CollectorDefine.APPSERVER_CLUSTER == 1) {
this.m_CollectorQueue.clear();
}
return 1;
}
public int initJMS(String m_AppserverType, String m_InitContextFactory,
String m_JmsURL, String m_JmsPort,
String m_TopicFactoryName, String m_TopicName) {
this.m_AppserverType = m_AppserverType;
if (m_AppserverType.toLowerCase().equals("ibm") == true) {
try {
Properties namingProps = new Properties();
namingProps.put(javax.naming.Context.PROVIDER_URL, m_JmsURL + m_JmsPort);
namingProps.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
m_InitContextFactory);
m_InitialContext = new InitialContext(namingProps);
m_TopicConnectionFactory = (TopicConnectionFactory) m_InitialContext.
lookup(m_TopicFactoryName);
m_Topic = (Topic) m_InitialContext.lookup(m_TopicName);
}
catch (NamingException e) {
return -1;
}
}
else
if (m_AppserverType.toLowerCase().equals("weblogic") == true) {
try {
//System.out.println(
// "in jmsfront, try to build one m_TopicConnection of jms");
Properties namingProps = new Properties();
namingProps.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
m_InitContextFactory);
namingProps.put(javax.naming.Context.PROVIDER_URL,
"t3://" + m_JmsURL + ":" + m_JmsPort);
/*namingProps.put(javax.naming.Context.PROVIDER_URL,
"iiop://" + m_JmsURL + ":" + m_JmsPort + "/");
*/
m_InitialContext = new InitialContext(namingProps);
m_Topic = (Topic) m_InitialContext.lookup(m_TopicName);
m_TopicConnectionFactory = (TopicConnectionFactory) m_InitialContext.
lookup(m_TopicFactoryName);
}
catch (NamingException e) {
//System.out.println("initJMS in JmsFront #1"+e.toString ());
return -1;
}
}
else {
try {
Properties namingProps = new Properties();
//env.put("java.naming.factory.m_InitialContext","com.sun.jndi.cosnaming.CNCtxFactory");
//env.put("java.naming.provider.url", "iiop://badger.prc.sun.com:3700");
namingProps.setProperty("java.naming.factory.m_InitialContext",
m_InitContextFactory);
namingProps.setProperty("org.omg.CORBA.ORBInitialHost", m_JmsURL);
namingProps.setProperty("org.omg.CORBA.ORBInitialPort", m_JmsPort);
m_InitialContext = new InitialContext(namingProps);
m_Topic = (Topic) m_InitialContext.lookup(m_TopicName);
m_TopicConnectionFactory = (TopicConnectionFactory) m_InitialContext.
lookup(m_TopicFactoryName);
}
catch (NamingException e) {
return -1;
}
}
try {
m_TopicConnection = m_TopicConnectionFactory.createTopicConnection();
m_TopicSession = m_TopicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
m_TopicPublisher = m_TopicSession.createPublisher(m_Topic);
m_TopicSubscriber = m_TopicSession.createSubscriber(m_Topic);
m_TopicSubscriber.setMessageListener(this);
m_TopicConnection.start();
}
catch (JMSException e) {
return -1;
}
CollectorDefine.SystemPrintln("Jms Init Success ! m_JmsURL:" +
m_JmsURL + "m_JmsPort :" + m_JmsPort +
"m_TopicFactoryName:" +
m_TopicFactoryName + "m_TopicName:" +
m_TopicName);
return 1;
}
public int reConnect() {
try {
if (m_TopicConnectionFactory == null) {
}
m_TopicConnection = m_TopicConnectionFactory.createTopicConnection();
m_TopicSession = m_TopicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
// this.m_TopicPublisher = this.m_TopicSession.createPublisher (this.m_TargetT);
// m_TopicSubscriber = m_TopicSession.createSubscriber (m_Topic);
// m_TopicSubscriber.setMessageListener (this);
m_TopicConnection.start();
}
catch (JMSException e) {
return -1;
}
return 1;
}
public int broadcast(JmsObject m_pJmsObject) {
int ret;
ObjectMessage m_ObjectMessage = null;
//System.out.println(" begin send msg");
if (this.m_TopicSession != null) {
try {
this.m_TopicPublisher = this.m_TopicSession.createPublisher(this.
m_Topic);
m_ObjectMessage = this.m_TopicSession.createObjectMessage();
m_ObjectMessage.setObject(m_pJmsObject);
this.m_TopicPublisher.publish(m_ObjectMessage);
this.m_TopicPublisher.close();