第一步:avtiveMq服务端的安装
博客网址: http://blog.csdn.net/chenxun_2010/article/details/52709277
http://sunbean.blog.51cto.com/972509/675529
avtiveMq服务端程序下载网址:http://activemq.apache.org/activemq-590-release.html
启动服务端程序:
tar -zxvf apache-activemq-5.9.0-bin.tar.gz
cd apache-activemq-5.9.0/bin
./activemq start (默认启动)
启动方式:
指定配置文件目录(在这个文件中可以自定义activemq使用的JDK路径,jvm参数等信息)
[root@localhost bin]# ./activemq setup /etc/default/activemq
这样会在/ect/default目录下生成activemq的配置文件,可利用vi /etc/default/activemq命令打开,并进行相应配置(例如修改JAVA_HOME)
加载自定义配置文件并启动
./activemq start xbean:./conf/activemq-demo.xml
bin/activemq start xbean:conf/activemq.xml
1、普通启动
./bin/activemq console (前台启动)
./activemq start (后台启动)
默认日志文件位置:apache-activemq-5.9.0/data/activemq.log
2 启动并指定日志文件
./activemq start >tmp/smlog
3、后台启动方式
[root@localhost bin]# nohup ./activemq start >/tmp/smlog
查看程序是否启动:
ps -elf | grep active
[wdm@bogon test]$ ps -elf | grep active
4 S root 2631 2598 0 80 0 - 38099 poll_s 16:33 ? 00:00:00 /usr/libexec/gdm-simple-slave --display-id.......
ActiveMQ默认采用61616端口提供JMS服务,使用8161端口提供管理控制台服务,执行以下命令以便检验是否已经成功启动ActiveMQ服务。
netstat -ln | grep 61
tcp 0 0 :::61613 :::* LISTEN
tcp 0 0 :::61614 :::* LISTEN
tcp 0 0 :::61616 :::* LISTEN
tcp 0 0 :::8161 :::* LISTEN
unix 2 [ ACC ] STREAM LISTENING 21043 /tmp/orbit-wdm/linc-c7d-0-6db2725613901
关闭防火墙
sudo service iptables stop
[sudo] password for wdm:
iptables:清除防火墙规则: [确定]
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:正在卸载模块: [确定]
监控
ActiveMQ默认启动时,启动了内置的jetty服务器,提供一个用于监控ActiveMQ的admin应用。
访问链接形式:http://127.0.0.1:8161/admin/
在浏览器中访问上面的链接(注意:ip修改成activeMq服务端的ip地址),出现登录界面,输入用户名和密码会进入activeMq管理网页页面(用户和密码都是admin)
停止ActiveMQ
第一种方式:
./activemq stop
第二种方式:
1、首先需要找到activemq进程的PID:
[root@localhost apache-activemq-5.5.0]# ps -ef | grep activemq
root 5259 1 0 21:49 ? 00:00:19 /usr/java/jdk1.6.0_27/bin/java -Xms256M .......
2、然后,杀死activemq的进程(其中 -9表示强制终止)
[root@localhost apache-activemq-5.5.0]# kill -9 5259
注意:若提示以下错误
ERROR: Configuration variable JAVA_HOME or JAVACMD is not defined correctly.
(JAVA_HOME='', JAVACMD='java')
代表java环境未安装,
解决办法:以centos下安装java为例:http://jingyan.baidu.com/article/4853e1e51d0c101909f72607.html
1、查看CentOS自带JDK是否已安装。
yum list installed |grep java。
2、 若有自带安装的JDK,如何卸载CentOS系统自带Java环境?(这里只是示例如何卸载,这里不需要卸载)
◆卸载JDK相关文件输入:yum -y remove java-1.7.0-openjdk*。
◆卸载tzdata-java输入:yum -y remove tzdata-java.noarch。
当结果显示为Complete!即卸载完毕。
注:“*”表示卸载掉java 1.7.0的所有openjdk相关文件。
3、 检查CentOS系统网络连接是否正常。
如:ping 百度URL即可
4、 查看yum库中的Java安装包。
◆输入:yum -y list java* 。
5、 使用yum安装Java环境。
◆输入:yum -y install java-1.7.0-openjdk* ,以yum库中java-1.7.0为例。
当结果显示为Complete!即安装完毕。
注:“*”表示将java-1.7.0的所有相关Java程序都安装上。
6、 查看刚安装的Java版本信息。
◆输入:java -version 可查看Java版本;
◆输入:javac 可查看Java的编译器命令用法(可略)。
第二步:activeMq cpp的编译安装(C++接口)
博客网址:http://blog.csdn.net/lgh1700/article/details/51055784
http://blog.csdn.net/chenxun_2010/article/details/52709277
依赖包下载网址:http://apache.fayea.com/apr/
安装依赖包(注意安装顺序):
(1)cppunit安装
wget https://sourceforge.net/projects/cppunit/files/cppunit/1.12.1/cppunit-1.12.1.tar.gz --no-check-certificate
tar -zxvf cppunit-1.12.1.tar.gz
cd cppunit-1.12.1
./configure --prefix=/usr/local/cppunit/
make
sudo make install
执行完后在/usr/local/cppunit/目录下可以看到头文件和库文件
(2)apr安装
这里选择最新的APR 1.5.2版本
tar -zxvf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr/
make
sudo make install
(3)apr-util安装
这里选择最新的APR-util 1.5.4版本
tar -zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/usr/local/aprutil --with-apr=/usr/local/apr/
make
sudo make install
(4)apr-iconv安装
这里选择最新的APR iconv 1.2.1版本
tar -zxvf apr-iconv-1.2.1.tar.gz
cd apr-iconv-1.2.1
./configure --prefix=/usr/local/apr-iconv/ --with-apr=/usr/local/apr/
make
sudo make install
(5)openssl安装
wget http://www.openssl.org/source/openssl-1.0.0a.tar.gz
tar -zxvf openssl-1.0.0a.tar.gz
cd openssl-1.0.0a
./config --prefix=/usr/local/openssl/
make
sudo make install
安装activemq
wget http://apache.fayea.com/activemq/activemq-cpp/3.9.3/activemq-cpp-library-3.9.3-src.tar.gz
tar -zxvf activemq-cpp-library-3.9.3-src.tar.gz
cd activemq-cpp-library-3.9.3
./configure --prefix=/usr/local/ActiveMQ-CPP --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/aprutil --with-cppunit=/usr/local/cppunit --with-openssl=/usr/local/openssl
make
sudo make instal
若在make时报错,提示找不到ssl库
/usr/bin/ld: cannot find -lssl
进入到 /usr/local/openssl目录,将lib64目录复制一份,复制的新目录名为lib,命令:cp -r lib64 lib,
这是由于make时使用的 /usr/local/openssl/lib路径导致。
测试程序编写(C++)
producer.cpp
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <decaf/lang/Long.h>
#include <decaf/util/Date.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/util/Config.h>
#include <activemq/library/ActiveMQCPP.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <memory>
using namespace activemq;
using namespace activemq::core;
using namespace decaf;
using namespace decaf::lang;
using namespace decaf::util;
using namespace decaf::util::concurrent;
using namespace cms;
using namespace std;
class SimpleProducer : public Runnable {
private:
Connection* connection;
Session* session;
Destination* destination;
MessageProducer* producer;
bool useTopic;
bool clientAck;
unsigned int numMessages;
std::string brokerURI;
std::string destURI;
private:
SimpleProducer( const SimpleProducer& );
SimpleProducer& operator= ( const SimpleProducer& );
public:
SimpleProducer( const std::string& brokerURI, unsigned int numMessages,
const std::string& destURI, bool useTopic = false, bool clientAck = false ) :
connection(NULL),
session(NULL),
destination(NULL),
producer(NULL),
useTopic(useTopic),
clientAck(clientAck),
numMessages(numMessages),
brokerURI(brokerURI),
destURI(destURI) {
}
virtual ~SimpleProducer(){
cleanup();
}
void close() {
this->cleanup();
}
virtual void run() {
try {
auto_ptr<ActiveMQConnectionFactory> connectionFactory(new ActiveMQConnectionFactory( brokerURI ) );
try{
connection = connectionFactory->createConnection();
connection->start();
} catch( CMSException& e ) {
e.printStackTrace();
throw e;
}
if( clientAck )
{
session = connection->createSession( Session::CLIENT_ACKNOWLEDGE );
} else
{
session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
}
if( useTopic )
{
destination = session->createTopic( destURI );
} else
{
destination = session->createQueue( destURI );
}
producer = session->createProducer( destination );
producer->setDeliveryMode( DeliveryMode::NON_PERSISTENT );
string threadIdStr = Long::toString( Thread::currentThread()->getId() );
string text = (string)"Hello world! from thread " + threadIdStr;
for( unsigned int ix=0; ix<numMessages; ++ix )
{
TextMessage* message = session->createTextMessage( text );
message->setIntProperty( "Integer", ix );
printf( "Sent message #%d from thread %s\n", ix+1, threadIdStr.c_str() );
producer->send( message );
delete message;
}
}catch ( CMSException& e ) {
e.printStackTrace();
}
}
private:
void cleanup(){
try{
if( destination != NULL ) delete destination;
}catch ( CMSException& e ) { e.printStackTrace(); }
destination = NULL;
try
{
if( producer != NULL ) delete producer;
}catch ( CMSException& e ) { e.printStackTrace(); }
producer = NULL;
try{
if( session != NULL ) session->close();
if( connection != NULL ) connection->close();
}catch ( CMSException& e ) { e.printStackTrace(); }
try{
if( session != NULL ) delete session;
}catch ( CMSException& e ) { e.printStackTrace(); }
session = NULL;
try{
if( connection != NULL ) delete connection;
}catch ( CMSException& e ) { e.printStackTrace(); }
connection = NULL;
}
};
int main(int argc , char* argv[])
{
activemq::library::ActiveMQCPP::initializeLibrary();
std::cout << "=====================================================\n";
std::cout << "Starting produce message:" << std::endl;
std::cout << "-----------------------------------------------------\n";
std::string brokerURI ="failover://(tcp://127.0.0.1:61616)";
unsigned int numMessages = 2000;
std::string destURI = "test.chen";
bool useTopics = false;
SimpleProducer producer( brokerURI, numMessages, destURI, useTopics );
producer.run();
producer.close();
std::cout << "-----------------------------------------------------\n";
std::cout << "Finished test" << std::endl;
std::cout << "=====================================================\n";
activemq::library::ActiveMQCPP::shutdownLibrary();
}
consumer.cpp
#include <decaf/lang/Thread.h>
#include <decaf/lang/Runnable.h>
#include <decaf/util/concurrent/CountDownLatch.h>
#include <activemq/core/ActiveMQConnectionFactory.h>
#include <activemq/core/ActiveMQConnection.h>
#include <activemq/transport/DefaultTransportListener.h>
#include <activemq/library/ActiveMQCPP.h>
#include <decaf/lang/Integer.h>
#include <activemq/util/Config.h>
#include <decaf/util/Date.h>
#include <cms/Connection.h>
#include <cms/Session.h>
#include <cms/TextMessage.h>
#include <cms/BytesMessage.h>
#include <cms/MapMessage.h>
#include <cms/ExceptionListener.h>
#include <cms/MessageListener.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace activemq;
using namespace activemq::core;
using namespace activemq::transport;
using namespace decaf::lang;
using namespace decaf::util;
using namespace decaf::util::concurrent;
using namespace cms;
using namespace std;
class SimpleAsyncConsumer : public ExceptionListener,
public MessageListener,
public DefaultTransportListener {
private:
Connection* connection;
Session* session;
Destination* destination;
MessageConsumer* consumer;
bool useTopic;
std::string brokerURI;
std::string destURI;
bool clientAck;
private:
SimpleAsyncConsumer( const SimpleAsyncConsumer& );
SimpleAsyncConsumer& operator= ( const SimpleAsyncConsumer& );
public:
SimpleAsyncConsumer( const std::string& brokerURI,
const std::string& destURI,
bool useTopic = false,
bool clientAck = false ) :
connection(NULL),
session(NULL),
destination(NULL),
consumer(NULL),
useTopic(useTopic),
brokerURI(brokerURI),
destURI(destURI),
clientAck(clientAck) {
}
virtual ~SimpleAsyncConsumer() {
this->cleanup();
}
void close() {
this->cleanup();
}
void runConsumer() {
try {
ActiveMQConnectionFactory* connectionFactory = new ActiveMQConnectionFactory( brokerURI );
connection = connectionFactory->createConnection();
delete connectionFactory;
ActiveMQConnection* amqConnection = dynamic_cast<ActiveMQConnection*>( connection );
if( amqConnection != NULL )
{
amqConnection->addTransportListener( this );
}
connection->start();
connection->setExceptionListener(this);
if( clientAck ) {
session = connection->createSession( Session::CLIENT_ACKNOWLEDGE );
} else {
session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
}
if( useTopic ) {
destination = session->createTopic( destURI );
} else {
destination = session->createQueue( destURI );
}
consumer = session->createConsumer( destination );
consumer->setMessageListener( this );
} catch (CMSException& e) {
e.printStackTrace();
}
}
virtual void onMessage( const Message* message ) {
static int count = 0;
try
{
count++;
const TextMessage* textMessage =
dynamic_cast< const TextMessage* >( message );
string text = "";
if( textMessage != NULL ) {
text = textMessage->getText();
} else {
text = "NOT A TEXTMESSAGE!";
}
if( clientAck ) {
message->acknowledge();
}
printf( "Message #%d Received: %s\n", count, text.c_str() );
} catch (CMSException& e) {
e.printStackTrace();
}
}
virtual void onException( const CMSException& ex AMQCPP_UNUSED ) {
printf("CMS Exception occurred. Shutting down client.\n");
exit(1);
}
virtual void transportInterrupted() {
std::cout << "The Connection's Transport has been Interrupted." << std::endl;
}
virtual void transportResumed() {
std::cout << "The Connection's Transport has been Restored." << std::endl;
}
private:
void cleanup(){
try{
if( destination != NULL ) delete destination;
}catch (CMSException& e) {}
destination = NULL;
try{
if( consumer != NULL ) delete consumer;
}catch (CMSException& e) {}
consumer = NULL;
try{
if( session != NULL ) session->close();
if( connection != NULL ) connection->close();
}catch (CMSException& e) {}
try{
if( session != NULL ) delete session;
}catch (CMSException& e) {}
session = NULL;
try{
if( connection != NULL ) delete connection;
}catch (CMSException& e) {}
connection = NULL;
}
};
int main(int argc, char* argv[]) {
activemq::library::ActiveMQCPP::initializeLibrary();
std::cout << "=====================================================\n";
std::cout << "Starting the example:" << std::endl;
std::cout << "-----------------------------------------------------\n";
std::string brokerURI = "failover:(tcp://127.0.0.1:61616)";
std::string destURI = "test.chen";
bool useTopics = false;
bool clientAck = false;
SimpleAsyncConsumer consumer( brokerURI, destURI, useTopics, clientAck );
consumer.runConsumer();
std::cout << "Press 'q' to quit" << std::endl;
while( std::cin.get() != 'q') {}
consumer.close();
std::cout << "-----------------------------------------------------\n";
std::cout << "Finished with the example." << std::endl;
std::cout << "=====================================================\n";
activemq::library::ActiveMQCPP::shutdownLibrary();
}
测试程序编译:
首先将动态库移到系统能够搜索到的目录下;
需要将libactivemq-cpp.so、libactivemq-cpp.so.19、libactivemq-cpp.so.19.0.3这3个运行时库复制到/usr/lib64目录下
编译
g++ producer.cpp -o send -I/usr/local/ActiveMQ-CPP/include/activemq-cpp-3.9.3 -I/usr/local/apr/include/apr-1 -L/usr/local/ActiveMQ-CPP/lib -lactivemq-cpp
g++ consumer.cpp -o receive -I/usr/local/ActiveMQ-CPP/include/activemq-cpp-3.9.3 -I/usr/local/apr/include/apr-1 -L/usr/local/ActiveMQ-CPP/lib -lactivemq-cpp
其中-I指定了两个include目录,-L指定了一个库目录,-l指定了一个链接库
测试程序的运行:
./send
./receive
ActiveMq的安装
最新推荐文章于 2021-04-29 13:00:51 发布