基于CMS接口的ActiveMQ CPP客户端示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#ifndef AMQUTILS_H_ #define AMQUTILS_H_ #include <cms/Connection.h> #include <cms/Session.h> #include <decaf/lang/Exception.h> using namespace decaf::lang; using namespace cms; namespace amqutils { inline void closeQuitely( Connection* conn, Session* session ) { if ( session ) try { session->close(); } catch ( Exception& e ) { } if ( conn ) try { conn->close(); } catch ( Exception& e ) { } } } #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#ifndef PRODUCER_H_ #define PRODUCER_H_ #include <activemq/core/ActiveMQConnectionFactory.h> #include <boost/shared_ptr.hpp> #include <stdlib.h> #include <stdio.h> 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; using namespace boost; class Producer { private: boost::shared_ptr<ActiveMQConnectionFactory> amqf; Connection* connection; Session* session; public: Producer( string& uri, string& userName, string& password ); Producer( boost::shared_ptr<ActiveMQConnectionFactory> amqf ); virtual ~Producer(); virtual void init(); virtual void send( string& queueName, string& msg ); virtual void pub( string& topicName, string& msg ); }; #endif |
10 years ago
0
基于JMS的ActiveMQ Java客户端示例
JMS生产者:
JMS消费者:
结合Spring使用,配置文件示例
监听器实现类示例
阅读全文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package cc.gmem.demo.amq.client; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class Producer { private ActiveMQConnectionFactory factory; public Producer( String brokerHost, int brokerPort ) { factory = new ActiveMQConnectionFactory( "tcp://" + brokerHost + ":" + brokerPort ); } public void sendMsgToQueue( String queueName, String msg ) throws JMSException { Connection connection = null; try { //如果代理启用身份验证,这里需要指定用户、密码参数 connection = factory.createConnection(); connection.start(); //创建会话时有事务和确认选项 Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE ); //如果代理上没有队列,则创建,否则,直接使用 Destination dest = session.createQueue( queueName ); //创建消息生产者 MessageProducer producer = session.createProducer( dest ); //设置消息是否持久化 producer.setDeliveryMode( DeliveryMode.NON_PERSISTENT ); //创建一个文本消息 TextMessage message = session.createTextMessage( msg ); producer.send( message ); } finally { connection.close(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package cc.gmem.demo.amq.client; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Consumer { private static final Logger LOGGER = LoggerFactory.getLogger( Consumer.class ); private ActiveMQConnectionFactory factory; public Consumer( String brokerHost, int brokerPort ) { factory = new ActiveMQConnectionFactory( "tcp://" + brokerHost + ":" + brokerPort ); } public String reveiveMsgFromQueue( String queueName ) throws JMSException { Connection connection = null; try { //如果代理启用身份验证,这里需要指定用户、密码参数 connection = factory.createConnection(); connection.start(); //创建会话时有事务和确认选项,如果确认选项为:SESSION_TRANSACTED,则必须启用事务 Session session = connection.createSession( true, Session.SESSION_TRANSACTED ); //如果代理上没有队列,则创建,否则,直接使用 Destination dest = session.createQueue( queueName ); //创建消息生产者 MessageConsumer consumer = session.createConsumer( dest ); //等待队列里面有消息可消费,最多1秒,超时返回NULL long timeout = 1000; TextMessage msg = (TextMessage) consumer.receive( timeout ); String recMsg = msg == null ? null : msg.getText(); //如果设置:Session.CLIENT_ACKNOWLEDGE,则必须手工确认:msg.acknowledge() session.commit();//如果设置:Session.SESSION_TRANSACTED,则必须提交或者回滚 return recMsg; } finally { connection.close(); } } } |
10 years ago
0
基于Eclipse ADT和Maven的Android开发
Android Studio目前作为Android官方的IDE,它使用Gradle作为构建工具,对Android平台引入的新特性都能够很快的支持。相比之下,Eclipse ADT则更新缓慢,与最常用的构建工具——Maven的集成主要依靠开源界的个人开发者的…
阅读全文
10 years ago
0
Ubuntu下安装GoogleDrive
安装必要的软件
设置同步路径
设置自动同步
Linux下目前没有Windows下的自动同步功能,可以使用crontab进行定时同步:
阅读全文
1 2 3 |
add-apt-repository ppa:nilarimogard/webupd8 apt-get update apt-get install grive |
1 2 |
#第一次使用要 -a参数,进行身份校验 grive -a -p /var/www |
10 years ago
0
Tomcat7配置指南
conf/server.xml
server.xml包含了影响Tomcat7的Servlet/JSP容器行为的绝大部分配置项。可以在该配置文件中使用Ant风格的变量替换。变量可以使用系统属性,对于propname,可以使用${propname}这种形式的占位…
阅读全文
11 years ago
0