如何在Node JS中为ActiveMQ编写发送者应用程序

时间:2015-02-18 05:18:58

标签: java node.js activemq stomp

我想为消息传递创建一个JMS发送者应用程序,并使用JAVA创建它。这是我在Java中的示例代码片段。

    try {
        factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        connection = factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        destination = session.createQueue("SAMPLEQUEUE");
        producer = session.createProducer(destination);
        try {

                TextMessage message = session.createTextMessage();
                message.setText("hello");
                producer.send(message);
                System.out.println("Sent: " + message.getText());

        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (JMSException e) {
        e.printStackTrace();
    }

这很好用,我也可以用接收器接收消息。我想在Node JS中更改发送器实现并使其成为Node JS应用程序。在Node JS中搜索ActiveMQ后,我不熟悉Node JS。任何指向它的指针都会非常有用。

此致 Subhankar

修改

我使用stomp作为节点JS。示例代码如下:

var Stomp = require('stomp-client');
var destination = '/queue/sensorstreamqueue';
var client = new Stomp('10.53.219.153', 61613, 'user', 'pass');
var     lazy    = require("lazy"),
        fs  = require("fs");



client.connect(function(sessionId) {
new lazy(fs.createReadStream('input.csv'))
     .lines
     .forEach(function(line){
         client.publish(destination, line.toString());
     }
 );   
    console.log("published");
});

代码工作,我的接收器也收到消息,但接收器希望它是textMesssage格式,并给出以下错误:

02-19-2015 08:42:31.288 ERROR [Thread-25] (JmsInputTransporter.handleTextMessage) Error code:401306, Severity : 3 (Error)
Error message:JMS Transporter is expected a TextMessage, received class org.apache.activemq.command.ActiveMQBytesMessage.
Error description:JMS Transporter is expected a TextMessage, received class org.apache.activemq.command.ActiveMQBytesMessage.

有人可以帮助我如何实现这一目标?

2 个答案:

答案 0 :(得分:4)

您可以尝试使用activemq-node模块,也可以在ActiveMQ上启用STOMP protocol并使用this node.js库。

答案 1 :(得分:0)

您需要添加标题:

client.publish(destination, 'your content', {
    "amq-msg-type": "text"
});

https://issues.apache.org/jira/browse/AMQ-2833