ActiveMQ:转换OpenWire和STOMP消息
创始人
2024-05-22 00:10:52
0

要在ActiveMQ中转换OpenWire和STOMP消息,可以按照以下步骤进行操作:

  1. 首先,需要确保你的ActiveMQ服务器已经启动。

  2. 创建一个Java项目,并添加ActiveMQ的相关依赖。例如,使用Maven可以添加以下依赖:


    org.apache.activemq
    activemq-core
    5.16.1

  1. 创建一个名为"OpenWireToStompConverter"的Java类,并在其中编写以下代码:
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQBytesMessage;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.activemq.transport.stomp.Stomp;
import org.apache.activemq.transport.stomp.StompFrame;
import org.apache.activemq.transport.stomp.StompFrameFactory;
import org.apache.activemq.transport.stomp.StompFrameTranslator;
import org.apache.activemq.transport.stomp.StompWireFormat;

import javax.jms.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class OpenWireToStompConverter {
    public static void main(String[] args) throws JMSException, IOException {
        // 创建OpenWire连接工厂
        ConnectionFactory openWireConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

        // 创建OpenWire连接
        Connection openWireConnection = openWireConnectionFactory.createConnection();
        openWireConnection.start();

        // 创建OpenWire会话
        Session openWireSession = openWireConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建OpenWire目标
        Destination openWireDestination = openWireSession.createQueue("TEST.FOO");

        // 创建OpenWire消息生产者
        MessageProducer openWireProducer = openWireSession.createProducer(openWireDestination);

        // 创建OpenWire消息
        TextMessage openWireMessage = openWireSession.createTextMessage("Hello OpenWire!");

        // 发送OpenWire消息
        openWireProducer.send(openWireMessage);

        // 创建Stomp连接工厂
        StompWireFormat stompWireFormat = new StompWireFormat();
        ConnectionFactory stompConnectionFactory = stompWireFormat.asConnectionFactory("tcp://localhost:61613");

        // 创建Stomp连接
        Connection stompConnection = stompConnectionFactory.createConnection();
        stompConnection.start();

        // 创建Stomp会话
        Session stompSession = stompConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建Stomp目标
        Destination stompDestination = stompSession.createQueue("TEST.FOO");

        // 创建Stomp消息消费者
        MessageConsumer stompConsumer = stompSession.createConsumer(stompDestination);

        // 创建Stomp消息监听器
        stompConsumer.setMessageListener(message -> {
            if (message instanceof ActiveMQMessage) {
                // 将OpenWire消息转换为Stomp消息
                ActiveMQMessage openWireMessage2 = (ActiveMQMessage) message;
                StompFrame stompFrame = convertToStompFrame(openWireMessage2);
                System.out.println("Received STOMP message: " + stompFrame.getBody());
            }
        });

        // 等待消息到达
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 关闭连接
        openWireConnection.close();
        stompConnection.close();
    }

    private static StompFrame convertToStompFrame(ActiveMQMessage openWireMessage) {
        StompFrame stompFrame = StompFrameFactory.createFrame(Stomp.Commands.MESSAGE);
        stompFrame.addHeader(Stomp.Headers.Message.MESSAGE_ID, openWireMessage.getJMSMessageID());
        stompFrame.addHeader(Stomp.Headers.Message.DESTINATION, openWireMessage.getJMSDestination().toString());
        stompFrame.addHeader(Stomp.Headers.CONTENT_TYPE, "text/plain");

        if (openWireMessage instanceof ActiveMQTextMessage) {
            ActiveMQTextMessage textMessage = (ActiveMQTextMessage) openWireMessage;
            try {
                stompFrame.setBody(textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        } else if (openWireMessage instanceof ActiveMQBytesMessage) {
            ActiveMQBytesMessage bytesMessage = (ActiveMQBytesMessage) openWireMessage;
            try {
                byte[] body = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage

相关内容

热门资讯

玻璃硬盘原理图 玻璃硬盘原理 玻璃硬盘,又称为磁头悬浮硬盘(Magnetic Head Flying Disk,MHFD),是一种...
闲鱼搜索规则与技巧 闲鱼最新特... 在闲鱼这个二手交易平台上,有很多用户都希望能够找到一些特殊的东西,比如一些罕见的收藏品、独特的手工艺...
家里监控最长能保存多少天的记录... 家里监控一般保存多久 随着科技的发展,家庭监控系统已经成为了许多家庭的必备设备,它不仅可以帮助我们...
QQ音乐提示代理模式可能无法正... QQ音乐提示代理模式可能无法正常访问,如上图所示,是怎么回事呢? 这个可能和你的网络设置有关系,首先...
别人打电话听不见我说话怎么回事... 当我们在使用手机时,可能会遇到别人打电话过来听不见声音的情况,这种情况可能是由多种原因导致的,下面我...
华为tag有用吗 华为tag-... 华为Tag是华为手机中的一种功能,它可以帮助用户更好地管理自己的手机数据和应用,通过使用华为Tag,...
ps5手柄可用手机快充充电吗 ... PS5手柄,即PlayStation 5的DualSense手柄,是索尼公司为PlayStation...
frp内网穿透配置 HTTP ... HTTP 类型的代理相比于 TCP 类型,不仅在服务端只需要监听一个额外的端口 vhost_http...
广电4k机顶盒怎么连接 广电网... 四广电网络,即四家主流的广播电视网络运营商,包括中国电信、中国移动、中国联通和中国广电,这些运营商为...
hwid是永久激活吗 hwid... HWID,全称Hardware ID,是硬件识别码的缩写,它是计算机硬件制造商为了区分每一台设备而分...