这段时间在写公司的综合互动平台,用户可以通过短信、IVR等实时参与互动节目,为了缩短服务器响应时间,减轻大量并发时给服务器带来的压力,项目使用了JMS异常消息通讯,在比较了几种开源的JMS中间件之后,我最后选择OpenJMS,相比其他几种来说,OpenJMS的配置更为简单,使用也方便,只不过功能相对简单一点。
消息发送端
package com.hing.tools;

import java.io.Serializable;
import java.util.Properties;

import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;


public class JMSTool ...{

public static void send(Object obj) ...{
String url = "tcp://localhost:3035";
String topic_name = "topic1";

try ...{
String factory_name = "ConnectionFactory";
Properties props = new Properties();

props
.put(Context.INITIAL_CONTEXT_FACTORY,
"org.exolab.jms.jndi.InitialContextFactory");
props.put(Context.PROVIDER_URL, url);

// lookup the connection factory from the context
Context context = new InitialContext(props);
TopicConnectionFactory factory = (TopicConnectionFactory) context
.lookup(factory_name);

// if we can't find the factory then throw an exception

if (factory != null) ...{
TopicConnection connection = factory
.createTopicConnection();
TopicSession session = connection
.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic topic = session
.createTopic(topic_name);


if (topic != null) ...{
TopicPublisher publisher = session
.createPublisher(topic);

if (publisher != null) ...{
Message msg=session.createObjectMessage((Serializable) obj);
publisher
.publish(msg);
}
}

// close the connction
connection.close();
}

} catch (Exception e) ...{
LogTool.error(JMSTool.class,e);

}
}
}
消息接收端
package com.hing.tools;

import java.util.Properties;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;


@SuppressWarnings("serial")

public class SMSMOConsumer implements MessageListener ...{

public SMSMOConsumer() ...{
String url = "tcp://localhost:3035";
String topic_name = "topic1";

try ...{
String factory_name = "ConnectionFactory";
Properties props = new Properties();

props
.put(Context.INITIAL_CONTEXT_FACTORY,
"org.exolab.jms.jndi.InitialContextFactory");
props.put(Context.PROVIDER_URL, url);

// lookup the connection factory from the context
Context context = new InitialContext(props);
TopicConnectionFactory factory = (TopicConnectionFactory) context
.lookup(factory_name);
Topic topic=(Topic) context.lookup(topic_name);

// if we can't find the factory then throw an exception

if (factory != null) ...{
TopicConnection connection = factory
.createTopicConnection();
TopicSession session = connection
.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
topic = (Topic) context.lookup("topic1");

// 创建Topic subscriber
TopicSubscriber topicSubscriber = session.createSubscriber(topic);
// 设置订阅监听
topicSubscriber.setMessageListener(this);

// 启动信息接收
connection.start();
LogTool.debug(this.getClass(),"start consumer listener");

}

} catch (Exception e) ...{
LogTool.error(this.getClass(),e);
}
}


public void onMessage(Message message) ...{
// TODO Auto-generated method stub

if(message instanceof ObjectMessage)...{
ObjectMessage obj=(ObjectMessage)message;
}
}

OpenJMS的配置文件内容
<?xml version="1.0"?>

<Configuration>

<!-- Optional. This represents the default configuration -->
<ServerConfiguration host="localhost" embeddedJNDI="true" />
<Connectors>
<Connector scheme="tcp">
<ConnectionFactories>
<ConnectionFactory name="ConnectionFactory" />
</ConnectionFactories>
</Connector>
<Connector scheme="rmi">
<ConnectionFactories>
<QueueConnectionFactory name="JmsQueueConnectionFactory" />
<TopicConnectionFactory name="JmsTopicConnectionFactory" />
</ConnectionFactories>
</Connector>
</Connectors>
<!-- Required -->
<DatabaseConfiguration>
<RdbmsDatabaseConfiguration
driver="org.apache.derby.jdbc.EmbeddedDriver"
url="jdbc:derby:openjmsdb;create=true"
user="openjms"
password="openjms"/>
</DatabaseConfiguration>

<!-- Required -->
<AdminConfiguration script="${openjms.home}instartup.bat" />
<!-- Optional. If not specified, no destinations will be created -->
<AdministeredDestinations>
<AdministeredTopic name="topic1">
<Subscriber name="smsmo" />
</AdministeredTopic>
<AdministeredTopic name="topic2">
<Subscriber name="smsmt" />
</AdministeredTopic>
<AdministeredQueue name="queue1" />
<AdministeredQueue name="queue2" />
<AdministeredQueue name="queue3" />
</AdministeredDestinations>

<!-- Optional. If not specified, no users will be created -->
<Users>
<User name="admin" password="openjms" />
</Users>

</Configuration>
