`
sillycat
  • 浏览: 2551290 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

NodeJS work with ActiveMQ(1)Basic Connect to Local ActiveMQ

 
阅读更多
NodeJS work with ActiveMQ(1)Basic Connect to Local ActiveMQ

Long time ago, I am using activeMQ, that is 15 years back, old days in Alibaba? Hudsun? Taobao? Alipay? China telecom? I can not remember, but that is really long time ago.
Today, I will try to use TypeScript under server less framework in AWS Lambda to connect to ActiveMQ as a producer. Yeah. Enjoy coding.

First of all, Set Up an ActiveMQ Server on Local
Old Old Home Page http://activemq.apache.org/
http://activemq.apache.org/web-console.html

I am using the Mac OS, so I will try the package http://apache.mirrors.pair.com//activemq/5.15.7/apache-activemq-5.15.7-bin.tar.gz
Unzip and move the file to the working directory
> sudo ln -s /Users/hluo/tool/apache-activemq-5.15.7 /opt/apache-activemq-5.15.7
> sudo ln -s /opt/apache-activemq-5.15.7 /opt/activemq

http://activemq.apache.org/getting-started.html

Start the service in the foreground
> cd /opt/activemq
> bin/activemq console
ActiveMQ WebConsole available at http://0.0.0.0:8161/
ActiveMQ Jolokia REST API available at http://0.0.0.0:8161/api/jolokia/

We can also start the activeMQ on the background
> bin/activemq start

Visit the web console here http://127.0.0.1:8161/admin/
We can easily create a queue and test message sending.

Command to stop the service
> bin/activemq stop

Connect to ActiveMQ with NodeJS
STOMP protocol - Streaming Text Orientated Message Protocol: SEND, SUBSCRIBE, UNSUBSCRIBE, BEGIN, COMMIT, ABORT, ACK, DISCONNECT.
On the official website, there is one I can try https://github.com/gdaws/node-stomp
API document is here http://gdaws.github.io/node-stomp/api/
Ports information http://activemq.apache.org/nms/stomp-uri-configuration.html

Default Port is 61616, JAVA Client is connecting as follow:
package com.sillycat.feeds2g;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQApp {
public static void main(String[] args) throws JMSException {
Connection connection;
Session session;
MessageProducer producer;
String url = "failover:(tcp://localhost:61616)";

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(session.createQueue("dwh_datawarehouse" + "," + "dwh_datawarehouse-raw"));
Message msg = session.createTextMessage("test connection to the server");
producer.send(msg);
producer.close();
session.close();
connection.close();
}
}

Package dependency is as follow:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.7</version>
</dependency>

STOMP in nodeJS is doing as follow:
var stompit = require('stompit');

var connectOptions = {
'host': 'localhost',
'port': 61613,
'timeout': 6000,
'connectHeaders':{
'host': '/',
//'login': 'username',
//'passcode': 'password',
'heart-beat': '5000,5000'
}
};

stompit.connect(connectOptions, function(error, client) {

if (error) {
console.log('connect error ' + error.message);
return;
}
var sendHeaders = {
'destination': '/queue/dwh_datawarehouse',
'content-type': 'text/plain'
};
var frame = client.send(sendHeaders);
frame.write('hello from nodejs' + new Date());
frame.end();
client.disconnect();
});

Here is the package dependency
"stompit": "0.26.0"

There are maybe more things need to care about, but we can check the documents here http://gdaws.github.io/node-stomp/api/

References:
http://activemq.apache.org/cross-language-clients.html
https://blog.csdn.net/qq_37398530/article/details/78140371
https://medium.com/@mackplevine/using-activemq-with-node-js-stomp-and-http-b251ce8d995
https://simplesassim.wordpress.com/2016/03/13/how-to-send-a-message-to-an-apache-activemq-queue-with-node-js/
STOMP
https://blog.csdn.net/chszs/article/details/5200554

Monitor tool
http://activemq.apache.org/how-can-i-monitor-activemq.html

100 years ago
http://sillycat.iteye.com/blog/563655
http://sillycat.iteye.com/blog/562675
http://sillycat.iteye.com/blog/1028467
https://www.soapui.org/documentation/jms/config.html
http://activemq.apache.org/hermes-jms.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics