使用 OpenMQ 4.5.2(独立运行,未嵌入 GlassFish)我用这个项目测试了消息转换
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.habarisoft</groupId>
<artifactId>OpenMQMessageTransformer</artifactId>
<version>1.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.glassfish.mq</groupId>
<artifactId>imq</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.mq</groupId>
<artifactId>jms</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.mq</groupId>
<artifactId>imqjmsbridge</artifactId>
<version>4.5.2</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
这个变压器类:
package com.habarisoft.mqtr;
import java.util.*;
import javax.jms.*;
import com.sun.messaging.bridge.service.MessageTransformer;
public class StompTransformer extends MessageTransformer<Message, Message> {
@Override
public Message transform(Message message,
boolean readOnly,
String charsetName,
String source,
String target,
Properties properties)
throws Exception {
System.out.println("transform ...");
Message m = message;
if (source.equals(SUN_MQ)) { //from Java Message Queue to STOMP client
if (message instanceof ObjectMessage) {
//create a new TextMessage for message to be transformed to
TextMessage tm = (TextMessage) createJMSMessage(JMSMessageType.TEXTMESSAGE);
tm.setText("<converted object>");
m = tm;
}
}
return m;
}
}
生成的 JAR 必须与其他所需的依赖 JAR 一起位于 lib/ext 目录中(请注意,这已经通过独立的 Open MQ 安装进行了测试)。
STOMP 桥的这个代理配置:
imq.bridge.admin.user=admin
imq.instanceconfig.version=300
imq.bridge.stomp.messageTransformer=com.habarisoft.mqtr.StompTransformer
imq.bridge.admin.password=admin
imq.bridge.activelist=stomp
imq.bridge.enabled=true
然后我启动了一个 STOMP 客户端监听队列 TOOL.DEFAULT 中的传入消息,并使用 JMS 客户端生成对象消息:
import java.util.Date;
import javax.jms.Queue;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
public class Main {
public static void main(String[] args) throws JMSException {
ConnectionFactory cf = new com.sun.messaging.ConnectionFactory();
Connection conn = cf.createConnection("admin", "admin");
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("TOOL.DEFAULT");
MessageProducer producer = session.createProducer(queue);
Date date = new Date();
ObjectMessage msg = session.createObjectMessage(date);
producer.send(msg);
conn.close();
}
}
如果 JMS 客户端代码在 STOMP 客户端正在侦听时发送对象消息,则代理运行成功的控制台窗口会写入
transform ...
信息。但随后它记录了一个错误,抱怨一个空的 JMS 目标:
Dez 10, 2014 8:04:39 AM
WARNUNG: [BSS2009]: Nachricht ID:8-192.168.56.1(ea:8a:dc:e5:15:7c)-58027-1418195
079856 kann nicht an Abonnent {98F4744E-6E12-44CC-8336-36A9BA7CDC77} ausgeliefer
t werden: JMS destination null !
javax.jms.JMSException: JMS destination null !
at com.sun.messaging.bridge.service.stomp.StompProtocolHandler.toStompDe
stination(StompProtocolHandler.java:832)
at com.sun.messaging.bridge.service.stomp.StompProtocolHandler.toStompFr
ameMessage(StompProtocolHandler.java:893)
at com.sun.messaging.bridge.service.stomp.StompConnection.toStompFrameMe
ssage(StompConnection.java:438)
at com.sun.messaging.bridge.service.stomp.StompSubscriberSession.onMessa
ge(StompSubscriberSession.java:127)
at com.sun.messaging.jmq.jmsclient.MessageConsumerImpl.deliverAndAcknowl
edge(MessageConsumerImpl.java:358)
at com.sun.messaging.jmq.jmsclient.MessageConsumerImpl.onMessage(Message
ConsumerImpl.java:287)
at com.sun.messaging.jmq.jmsclient.SessionReader.deliver(SessionReader.j
ava:119)
at com.sun.messaging.jmq.jmsclient.ConsumerReader.run(ConsumerReader.jav
a:192)
at java.lang.Thread.run(Thread.java:745)
Dez 10, 2014 8:04:39 AM
我没有对此进行进一步调查,但看起来构建转换后的消息所需的代码比OpenMQ JavaDoc 中为转换器显示的要多。
但是测试结果表明调用了transformer,并且可以使用提供的示例作为起点。
更新:
如果系统架构在您的控制之下,那么在消息流中插入 Java (JMS) 客户端应用程序可能会更容易,它将对象消息转换为文本消息并将其重新发送到不同的目的地,STOMP客户消费。