最近公司有个需求,需要接收其他部门发送的MQ请求,发送MQ的部门是基于pub/sub方式发布的。我负责我们部门的订阅MQ的开发。
环境:websphere6.1
实现:a) 采用Message Driver Bean
在WAS上部署一个EJB应用,将业务逻辑在MDB的onMessage () 方法中实现。
b) 采用JMS程序
自己开发轮询程序,获得消息内容,并将其用于业务逻辑中。
开发细节:
1.在websphere上配置消息中间件:
a.新建主题工厂
主机、端口配置成发布消息的地址,传输类型选择client,通道,队列管理器填写。代理版本选择基本,客户机标识填写接收消息的标识
b.新建主题
基本主题名填写a中的客户机标识,目标客户选择jms
c.配置监听端口
服务器---应用服务器----server1----通信----消息侦听器服务----侦听器端口---新建侦听端口侦听主题工厂、主题。
2.两种实现订阅消息
a .jsm轮询查询:
public static void main(String[] args) { JMSPubSub.send(args);
}
public static String send(String[] args){
String result = null;
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
System.out.println(args[3]);
System.out.println(args[4]);
if (args.length < 4) {
usage();
System.out.println("out");
return result;
}
String subName = null;
String url = args[0];
boolean isPublisher = true;
if (args[3].equalsIgnoreCase("P")) {
isPublisher = true;;
} else if (args[3].equalsIgnoreCase("S")) {
isPublisher = false;
if (args.length > 4) subName = args[4];
} else {
usage();
return result;
}
javax.naming.InitialContext initContext;
initContext = initContext(url);
System.out.println("initContext:" + initContext);
if (initContext == null) return result;
if (true) {
TopicConnection topicConnection = initTopicConnection(initContext, args[1]);
if (topicConnection == null) return result;
try {
topicConnection.start();
System.out.println("topicConnection start......");
} catch (JMSException e1) {
System.out.println("topicConnection start is error......");
e1.printStackTrace();
return result;
}
TopicSession session;
try {
session = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
System.out.println("topicConnectionSession start......");
} catch (JMSException e1) {
System.out.println("topicConnectionSession start is error......");
e1.printStackTrace();
return result;
}
Topic topic = getTopic(initContext, args[2]);
if (session == null) return result;
if (isPublisher) {
try {
TopicPublisher publisher = session.createPublisher(topic);
System.out.println("TopicPublisher start......");
TextMessage message = session.createTextMessage();
String text = getFromIn();
message.setText(text);
System.out.println("publisher start......");
publisher.publish(message);
System.out.println("publisher end......");
} catch (JMSException e) {
System.out.println("publisher is error ......");
e.printStackTrace();
}
} else {
try {
System.out.println("in sub ......");
TopicSubscriber subscriber;
if (subName != null && !subName.equals("")) {
subscriber = session.createDurableSubscriber(topic, subName);
} else {
subscriber = session.createSubscriber(topic);
}
System.out.println("createDurableSubscriber over ......");
// MessageListener listener = new JMSPubSub.MyMessageListener();
Message arg0 = subscriber.receive();
if (arg0 instanceof TextMessage) {
try {
result = ((TextMessage) arg0).getText();
System.out.println("message: [" +result + "]");
} catch (JMSException e) {
System.out.println("get message is error" + e);
e.printStackTrace();
}
} else {
System.out.println("Message Type is error.");
}
} catch (JMSException e) {
System.out.println("createDurableSubscriber is error ......");
e.printStackTrace();
}
}
if (session != null) {
try {
session.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (topicConnection != null) {
try {
topicConnection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return result;
}
传入参数:iiop://10.1.12.11:2810 notification/IdentificationChangedTopicFactory notification/IdentificationChangedTopic S identificationChanged
其中出现错误:1.远程调用ejb查找jndi失败问题。2.websphere dumpNameSpace 查找jndi要求输入服务器登录用户名密码。3.调用接收程序之后,重复调用发订阅信息被锁的问题。
b. 采用Message Driver Bean,由websphere自己订阅接收到的信息。需要在websphere上部署一个ejb-mdb。ejb2.1写法如下:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>
identychangeEJB</display-name>
<enterprise-beans>
<message-driven id="SubscriberDMDB">
<display-name>
SubscriberMDB_IdentyChange</display-name>
<ejb-name>SubscriberMDB_IdentyChange</ejb-name>
<ejb-class>com.taikang.identychange.ejb.IdentificationChangedMDB</ejb-class>
<messaging-type>javax.jms.MessageListener</messaging-type>
<transaction-type>Bean</transaction-type>
<message-destination-type>javax.jms.Topic</message-destination-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>acknowledgeMode</activation-config-property-name>
<activation-config-property-value>Auto-acknowledge</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Topic</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>subscriptionDurability</activation-config-property-name>
<activation-config-property-value>Durable</activation-config-property-value>
</activation-config-property>
</activation-config>
<env-entry>
<env-entry-name>jndi-datasource-name</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>jdbc/InsureDB</env-entry-value>
</env-entry>
<resource-ref id="ResourceRef_1254203818020">
<description>Database reference for insure application</description>
<res-ref-name>jdbc/InsureDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</message-driven>
</enterprise-beans>
</ejb-jar>
遇到问题:1.ejb部署问题。2.ejb引用其他jar包问题。3.ejb使用jndi的问题。
4.最奇怪的:transaction问题:发生试探非法用现有两阶可用资源落实一阶可用资源。
通过<transaction-type>Bean</transaction-type>来解决的。(之前是<transaction-type>Container</transaction-type>)
最后测试通过!!!!!!!!希望对他人有用,传上订阅的ear包。
分享到:
相关推荐
websphere 6.1安装配置指南websphere 6.1安装配置指南websphere 6.1安装配置指南websphere 6.1安装配置指南websphere 6.1安装配置指南websphere 6.1安装配置指南
was相关资料: Websphere安装步骤 websphere安装后配置说明 Websphere实用手册 WebSphere+v6.1配置文档
MyEclipse 配置 Websphere6.1
Websphere6.1 安装 配置 DataSource JDBC JNDI ORACLE
WebSphere 6.1 SSL配置文档
WebSphere Application Server Network Deployment 6集群安装部署步骤.docxwebsphere6.1集群配置
1.MyEclipse6.0中Websphere6.1的配置; 2.Websphere6.1基本配置.
IBM WebSphere 6.1 ND 集群安装配置
WebSphere6.1 linux windowns 安装部署手册
websphere6.1.0.0升级至6.1.0.17
WebSphere 6.1应用服务器安装部署手册
相比于tomcat的项目部署,websphere中项目的部署还是还是很不一样的,本文档中图文并茂地讲述了,在Myeclipse中如何配置websphere以及如何部署web程序 另外,还详细讲解了如何在websphere中配置数据库连接池
介绍Websphere6.1程序部署,安装,配置,程序包发布等。
WebSphere6.1配置 在WebSphere6.1下发布web应用,数据库连接池的配置
Websphere6.1集群安装和配置 Websphere6.1集群安装和配置 Websphere6.1集群安装和配置
WebSpere集群配置\WebSphere6.1安装部署手册.doc
websphere 6.1 jms配置 包括总线的设置, 连接工厂的设置,队列的设置以及激活规范的设置等
NULL 博文链接:https://happyjin2010.iteye.com/blog/785173
比较全面和易懂的介绍了webshpere5.1上,部署应用和配置数据库联接等。很适合于初学者。
图解说明如何安装websphere6.1