jacky的部落 2018-11-07
NodeJSworkwithActiveMQ(1)BasicConnecttoLocalActiveMQ
Longtimeago,IamusingactiveMQ,thatis15yearsback,olddaysinAlibaba?Hudsun?Taobao?Alipay?Chinatelecom?Icannotremember,butthatisreallylongtimeago.
Today,IwilltrytouseTypeScriptunderserverlessframeworkinAWSLambdatoconnecttoActiveMQasaproducer.Yeah.Enjoycoding.
Firstofall,SetUpanActiveMQServeronLocal
OldOldHomePagehttp://activemq.apache.org/
http://activemq.apache.org/web-console.html
IamusingtheMacOS,soIwilltrythepackagehttp://apache.mirrors.pair.com//activemq/5.15.7/apache-activemq-5.15.7-bin.tar.gz
Unzipandmovethefiletotheworkingdirectory
>sudoln-s/Users/hluo/tool/apache-activemq-5.15.7/opt/apache-activemq-5.15.7
>sudoln-s/opt/apache-activemq-5.15.7/opt/activemq
http://activemq.apache.org/getting-started.html
Starttheserviceintheforeground
>cd/opt/activemq
>bin/activemqconsole
ActiveMQWebConsoleavailableathttp://0.0.0.0:8161/
ActiveMQJolokiaRESTAPIavailableathttp://0.0.0.0:8161/api/jolokia/
WecanalsostarttheactiveMQonthebackground
>bin/activemqstart
Visitthewebconsoleherehttp://127.0.0.1:8161/admin/
Wecaneasilycreateaqueueandtestmessagesending.
Commandtostoptheservice
>bin/activemqstop
ConnecttoActiveMQwithNodeJS
STOMPprotocol-StreamingTextOrientatedMessageProtocol:SEND,SUBSCRIBE,UNSUBSCRIBE,BEGIN,COMMIT,ABORT,ACK,DISCONNECT.
Ontheofficialwebsite,thereisoneIcantryhttps://github.com/gdaws/node-stomp
APIdocumentisherehttp://gdaws.github.io/node-stomp/api/
Portsinformationhttp://activemq.apache.org/nms/stomp-uri-configuration.html
DefaultPortis61616,JAVAClientisconnectingasfollow:
packagecom.sillycat.feeds2g;
importjavax.jms.Connection;
importjavax.jms.ConnectionFactory;
importjavax.jms.JMSException;
importjavax.jms.Message;
importjavax.jms.MessageProducer;
importjavax.jms.Session;
importorg.apache.activemq.ActiveMQConnectionFactory;
publicclassActiveMQApp{
publicstaticvoidmain(String[]args)throwsJMSException{
Connectionconnection;
Sessionsession;
MessageProducerproducer;
Stringurl="failover:(tcp://localhost:61616)";
ConnectionFactoryconnectionFactory=newActiveMQConnectionFactory(url);
connection=connectionFactory.createConnection();
connection.start();
session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
producer=session.createProducer(session.createQueue("dwh_datawarehouse"+","+"dwh_datawarehouse-raw"));
Messagemsg=session.createTextMessage("testconnectiontotheserver");
producer.send(msg);
producer.close();
session.close();
connection.close();
}
}
Packagedependencyisasfollow:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.7</version>
</dependency>
STOMPinnodeJSisdoingasfollow:
varstompit=require('stompit');
varconnectOptions={
'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('connecterror'+error.message);
return;
}
varsendHeaders={
'destination':'/queue/dwh_datawarehouse',
'content-type':'text/plain'
};
varframe=client.send(sendHeaders);
frame.write('hellofromnodejs'+newDate());
frame.end();
client.disconnect();
});
Hereisthepackagedependency
"stompit":"0.26.0"
Therearemaybemorethingsneedtocareabout,butwecancheckthedocumentsherehttp://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
Monitortool
http://activemq.apache.org/how-can-i-monitor-activemq.html
100yearsago
https://www.soapui.org/documentation/jms/config.html
http://activemq.apache.org/hermes-jms.html