一、源码如下:【存成amq_cli.cpp】
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#include
#include
#include
#include
#include
#include
using namespace activemq::core;
using namespace decaf::util::concurrent;
using namespace decaf::util;
using namespace decaf::lang;
using namespace cms;
using namespace std;
string gettext()
{
string
text="hello ttsky!!";
return text;
}
void sentmessage()
{
activemq::library::ActiveMQCPP::initializeLibrary();
std::auto_ptr<:connectionfactory>
connectionFactory(
cms::ConnectionFactory::createCMSConnectionFactory(
"tcp://192.168.1.152:61616" ) );
//建立连接工厂
while(true)
{
Connection* connection =
connectionFactory->createConnection();
connection->start();//创建连接
printf("connect a messagern");
Session* session =
connection->createSession(cms::Session::AUTO_ACKNOWLEDGE
);
//创建线程
string text = gettext();
TextMessage* message = session->createTextMessage(
text );//创建消息
Destination* destination;
destination=NULL;
destination = session->createQueue( "ttsky"
);//创建队列
MessageProducer*
myproducer=session->createProducer(destination
);
myproducer->setDeliveryMode(
DeliveryMode::NON_PERSISTENT );//消息的持久性
// long long startTime = Date::getCurrentTimeMilliseconds();
myproducer->send(message );//发送消息
printf("send a messagern");
session->close();
connection->close();
// long long
endTime = Date::getCurrentTimeMilliseconds();
// double
totalTime = (endTime - startTime) / 1000.0;
// std::cout
<< "Time to completion = "
<< totalTime
<< " seconds."
<< std::endl;
sleep(5);
}
activemq::library::ActiveMQCPP::shutdownLibrary();
}
int main(int argc AMQCPP_UNUSED, char* argv[] AMQCPP_UNUSED)
{
sentmessage();
}
二、编译
[root@localhost
Desktop]# g++ -o amq_cli amq_cli.cpp -lactivemq-cpp
当然,编译之前还需要安装下面几个库:
1、apr-1.4.2.tar.gz
2、apr-util-1.3.10.tar.gz
3、activemq-cpp-library-3.2.4-src.tar.gz
其中,以上第2个工具安装的时候,要用 ./configure --with-apr=/usr/local/apr,指明第1个工具apr安装的路径,才能产生makefile文件。
如果编译时找不到头文件, 这样搞一下:
[root@m176com activemq-cpp-3.2.4]# cp ./cms
/usr/local/include/ -R
[root@m176com activemq-cpp-3.2.4]# cp ./decaf/
/usr/local/include/ -R [root@m176com activemq-cpp-3.2.4]# cp ./activemq/
/usr/local/include/ -R
三、运行:
运行时找不到动态库。
[root@localhost Desktop]# ./amq_cli
./amq_cli: error while loading shared libraries:
libactivemq-cpp.so.12: cannot open shared object file: No such file
or directory
在/etc/ld.so.conf中加入 libactivemq-cpp.so.12 所在的目录
[root@localhost
Desktop]# cat
/etc/ld.so.conf
/usr/local/lib[root@localhost Desktop]# ldconfig
再运行,就OK了。