Repository: yanghua/banyan Branch: master Commit: eff18b0d28e1 Files: 177 Total size: 488.5 KB Directory structure: gitextract_25jgdlw4/ ├── .gitignore ├── .travis.yml ├── README.md ├── benchmark/ │ ├── README-CN.md │ ├── pom.xml │ └── src/ │ └── main/ │ ├── java/ │ │ └── com/ │ │ └── messagebus/ │ │ └── benchmark/ │ │ └── client/ │ │ ├── Benchmark.java │ │ ├── IFetcher.java │ │ ├── ILifeCycle.java │ │ ├── TestConfigConstant.java │ │ ├── TestMessageFactory.java │ │ ├── TestUtility.java │ │ └── testCase/ │ │ ├── CalcService.java │ │ ├── ConsumeTestCase.java │ │ ├── MessagebusJSONRpcTestCase.java │ │ ├── OriginalConsumeTestCase.java │ │ ├── OriginalProduceTestCase.java │ │ ├── OriginalProduceWithoutTopologyTestCase.java │ │ ├── ProduceRemainLogTestCase.java │ │ ├── ProduceTestCase.java │ │ ├── ProduceWithoutLogTestCase.java │ │ └── ThriftJSONRpcTestCase.java │ └── resources/ │ ├── clearEnv.sh │ ├── log4j.properties │ ├── plotscript_consume.sh │ ├── plotscript_messagebus_client_produce.sh │ ├── plotscript_messagebus_client_remain_log_produce.sh │ ├── plotscript_messagebus_client_without_realtime_log_produce.sh │ ├── plotscript_official_client_produce.sh │ └── plotscript_official_client_without_topology_produce.sh ├── client/ │ ├── pom.xml │ └── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com/ │ │ │ └── messagebus/ │ │ │ └── client/ │ │ │ ├── AbstractPool.java │ │ │ ├── Address.java │ │ │ ├── ConfigManager.java │ │ │ ├── GenericContext.java │ │ │ ├── IMessageReceiveListener.java │ │ │ ├── IRequestListener.java │ │ │ ├── IRpcMessageProcessor.java │ │ │ ├── InnerClient.java │ │ │ ├── InnerPool.java │ │ │ ├── MessageContext.java │ │ │ ├── MessageResponseTimeoutException.java │ │ │ ├── Messagebus.java │ │ │ ├── MessagebusConnectedFailedException.java │ │ │ ├── MessagebusFactory.java │ │ │ ├── MessagebusPool.java │ │ │ ├── MessagebusSinglePool.java │ │ │ ├── MessagebusUnOpenException.java │ │ │ ├── WrappedRpcServer.java │ │ │ ├── carry/ │ │ │ │ ├── AbstractMessageCarryer.java │ │ │ │ ├── CarryFactory.java │ │ │ │ ├── GenericBroadcaster.java │ │ │ │ ├── GenericConsumer.java │ │ │ │ ├── GenericProducer.java │ │ │ │ ├── GenericPublisher.java │ │ │ │ ├── GenericRequester.java │ │ │ │ ├── GenericResponser.java │ │ │ │ ├── GenericRpcRequester.java │ │ │ │ ├── GenericRpcResponser.java │ │ │ │ ├── GenericSubscriber.java │ │ │ │ ├── IBroadcaster.java │ │ │ │ ├── IConsumer.java │ │ │ │ ├── IProducer.java │ │ │ │ ├── IPublisher.java │ │ │ │ ├── IRequester.java │ │ │ │ ├── IResponser.java │ │ │ │ ├── IRpcRequester.java │ │ │ │ ├── IRpcResponser.java │ │ │ │ └── ISubscriber.java │ │ │ ├── event/ │ │ │ │ ├── carry/ │ │ │ │ │ ├── BroadcastEventProcessor.java │ │ │ │ │ ├── CarryEvent.java │ │ │ │ │ ├── CommonEventProcessor.java │ │ │ │ │ ├── ConsumeEventProcessor.java │ │ │ │ │ ├── ProduceEventProcessor.java │ │ │ │ │ ├── PublishEventProcessor.java │ │ │ │ │ ├── RequestEventProcessor.java │ │ │ │ │ ├── ResponseEventProcessor.java │ │ │ │ │ ├── RpcRequestEventProcessor.java │ │ │ │ │ ├── RpcResponseEventProcessor.java │ │ │ │ │ └── SubscribeEventProcessor.java │ │ │ │ └── component/ │ │ │ │ ├── ClientDestroyEvent.java │ │ │ │ ├── ClientDestroyEventProcessor.java │ │ │ │ ├── ClientInitedEvent.java │ │ │ │ ├── ClientInitedEventProcessor.java │ │ │ │ ├── InnerEvent.java │ │ │ │ └── NoticeEvent.java │ │ │ ├── extension/ │ │ │ │ └── thrift/ │ │ │ │ ├── TAMQPClientTransport.java │ │ │ │ └── ThriftMessageHandler.java │ │ │ ├── message/ │ │ │ │ ├── model/ │ │ │ │ │ ├── Message.java │ │ │ │ │ ├── MessageFactory.java │ │ │ │ │ └── MessageJSONSerializer.java │ │ │ │ └── transfer/ │ │ │ │ └── MessageHeaderTransfer.java │ │ │ └── model/ │ │ │ ├── BaseModel.java │ │ │ ├── Config.java │ │ │ ├── HandlerModel.java │ │ │ └── MessageCarryType.java │ │ └── resources/ │ │ └── log4j.properties │ └── test/ │ └── java/ │ └── com/ │ └── messagebus/ │ └── client/ │ ├── api/ │ │ ├── Broadcast.java │ │ ├── ComponentEventListener.java │ │ ├── ProduceConsume.java │ │ ├── ProduceConsumeLoopback.java │ │ ├── PublishSubscribe.java │ │ ├── RequestResponse.java │ │ └── RpcRequestResponse.java │ ├── core/ │ │ ├── BaseTestCase.java │ │ └── MessageUtil.java │ └── feature/ │ ├── CalcService.java │ ├── CalcServiceImpl.java │ ├── MessageBodySize.java │ ├── MessageNumLimit.java │ └── ThriftWithAMQPRpc.java ├── common-component/ │ ├── pom.xml │ └── src/ │ └── main/ │ ├── java/ │ │ └── com/ │ │ └── messagebus/ │ │ └── common/ │ │ ├── AuthInfo.java │ │ ├── Constants.java │ │ ├── Event.java │ │ ├── ExceptionHelper.java │ │ ├── GsonUtil.java │ │ ├── HttpHelper.java │ │ ├── IAsyncCallback.java │ │ ├── InnerNoticeEntity.java │ │ ├── NumberHelper.java │ │ ├── RandomHelper.java │ │ ├── RouterType.java │ │ ├── ShellHelper.java │ │ ├── TestVariableInfo.java │ │ ├── UUIDGenerator.java │ │ └── compress/ │ │ ├── CompressEnum.java │ │ ├── CompressorFactory.java │ │ ├── ICompressor.java │ │ ├── LZFCompressor.java │ │ └── SnappyCompressor.java │ └── resources/ │ └── log4j.properties ├── httpbridge/ │ ├── README.md │ ├── pom.xml │ └── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com/ │ │ │ └── messagebus/ │ │ │ └── httpbridge/ │ │ │ ├── controller/ │ │ │ │ ├── ExceptionHandler.java │ │ │ │ └── HttpBridge.java │ │ │ ├── filter/ │ │ │ │ └── URLDispatcher.java │ │ │ ├── listener/ │ │ │ │ └── AppContextListener.java │ │ │ ├── model/ │ │ │ │ └── TextMessage.java │ │ │ └── util/ │ │ │ ├── CommonUtil.java │ │ │ ├── Constants.java │ │ │ ├── ResponseUtil.java │ │ │ └── TextMessageJSONSerializer.java │ │ ├── resources/ │ │ │ └── log4j.properties │ │ └── webapp/ │ │ └── WEB-INF/ │ │ ├── jetty-web.xml │ │ └── web.xml │ └── test/ │ ├── java/ │ │ └── com/ │ │ └── messagebus/ │ │ └── httpbridge/ │ │ └── util/ │ │ └── PropertiesHelper.java │ └── resource/ │ └── common.properties ├── interactor-component/ │ ├── pom.xml │ └── src/ │ └── main/ │ └── java/ │ └── com/ │ └── messagebus/ │ └── interactor/ │ ├── proxy/ │ │ ├── ProxyConsumer.java │ │ └── ProxyProducer.java │ ├── rabbitmq/ │ │ ├── AbstractInitializer.java │ │ ├── Exchange.java │ │ ├── ExchangeManager.java │ │ ├── MQDataInitializer.java │ │ ├── Queue.java │ │ ├── QueueManager.java │ │ └── RabbitmqServerManager.java │ └── util/ │ └── ShellHelper.java ├── managesystem/ │ └── banyan_DB.sql ├── pom.xml └── scenario/ ├── pom.xml └── src/ └── main/ ├── java/ │ └── com/ │ └── messagebus/ │ └── scenario/ │ ├── client/ │ │ ├── BroadcastSubscribe.java │ │ ├── ClientUnderMultiThread.java │ │ ├── ProduceConsume.java │ │ ├── PublishSubscribe.java │ │ ├── RequestResponse.java │ │ └── XSLTTestCase.java │ ├── httpBridge/ │ │ ├── ProduceConsume.java │ │ ├── PublishSubscribe.java │ │ ├── RequestTemplate.java │ │ └── ResponseTemplate.java │ └── util/ │ └── PropertiesHelper.java └── resources/ ├── common.properties ├── consumerSchema.xml ├── pToc.xsl ├── producerSchema.xml └── producerXml.xml ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .idea/* .DS_Store */target/* *.iml META-INF/* *.rdb *.MF *.class ================================================ FILE: .travis.yml ================================================ language: java ================================================ FILE: README.md ================================================ #overview [![Build Status](https://travis-ci.org/yanghua/banyan.svg?branch=messagebus-ng)](https://travis-ci.org/yanghua/banyan) ![img 15][15] ``` banyan is a tree has thick branches which grows in the East Asia. ``` Thanks for [@ok95](https://github.com/ok95) & [@Joy-Zhang](https://github.com/Joy-Zhang) given the good idea & guidance. ![img 14][14] banyan used to communicate and integrate over multi-app. It depends on [RabbitMQ](http://www.rabbitmq.com/) as backend broker(message exchanger). Most scenario: * enterprise information Integration * oriented-component & oriented-module distributed developer * infrastructure for esb or soa * json-rpc / IDL-RPC(thrift) * configured compress/uncompress * http-bridge for all platform * web-console UI the necessity of encapsulating with RabbitMQ: * provide router pattern * embed permission into client-jar * removed create & delete & update operation from client, replaced with central-register mode more information please see : [banyan-category](http://blog.csdn.net/column/details/banyan.html) ##tree topology structure the message bus's implementation is based on Rabbitmq. It can takes advantage of multiple message exchange-types rabbitmq provided and builds many kinds of router pattern. The message bus's router topology lists below: ![img 3][3] the advantages of the tree topology: * hide the router topology from client-caller (just need to know the `proxy` node) * multiple message communication pattern (p2p, pub/sub, broadcast) * implement the message log without interrupting the message channel * communication-policy configure once , push everywhere ##web-console banyan has its' own web console that built as a Apache-ofbiz's component. The web console provide a dashboard about rabbitmq : ![img 16][16] and some core model's maintenance such as queue : ![img 17][17] ##Restful API * [apis](https://github.com/yanghua/banyan/tree/master/httpbridge) ##scenario & usage ###produce & consume * [produce](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FProduceConsume.java#L34) / [pull consume](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FProduceConsume.java#L46) * [produce](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FProduceConsume.java#L34) / [push consume](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FProduceConsume.java#L64) * [produce](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FProduceConsume.java#L34) / [async consume with another thread](https://github.com/yanghua/banyan/blob/master/scenario%2Fsrc%2Fmain%2Fjava%2Fcom%2Fmessagebus%2Fscenario%2Fclient%2FProduceConsume.java#L91) * [produce-loopback](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FProduceConsumeLoopback.java#L15) / [consume-loopback](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FProduceConsumeLoopback.java#L15) ###request & response * [request](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FRequestResponse.java#L35) / [response](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FRequestResponse.java#L35) ###publish & subscirbe * [publish](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FPublishSubscribe.java#L31) / [subscribe](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FPublishSubscribe.java#L31) ###broadcast & notification-handler * [broadcast](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FBroadcast.java#L31) / [notification-handler](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FBroadcast.java#L31) ###json-rpc(wrapped-offical-java-client) * [rpc-request](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FRpcRequestResponse.java#L28) / [rpc-response](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Fapi%2FRpcRequestResponse.java#L28) ###thrift-rpc(thrid-party-rpc-integrated) * [rpc-request](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Ffeature%2FThriftWithAMQPRpc.java#L36) / [rpc-response](https://github.com/yanghua/banyan/blob/master/client%2Fsrc%2Ftest%2Fjava%2Fcom%2Fmessagebus%2Fclient%2Ffeature%2FThriftWithAMQPRpc.java#L59) ###http-restful * [http-apis](https://github.com/yanghua/banyan/tree/master/scenario/src/main/java/com/messagebus/scenario/httpBridge) ##benchmark it shows the `client` performance: ###hardware client : ``` OS : Mac os x Yosemite (version 10.10) Processor : 2.5GHz Intel Core i5 Memory : 8GB 1600 MHz DDR3 JDK Version : 1.7.0_45 ``` server : ``` OS : Ubuntu Server 14.04.1 (GNU/Linux 3.13.0-37-generic x86_64) Processor : Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz (8核) Memory : 8GB JDK Version : 1.7.0_72 ``` ###produce * single thread,multiple message size ,cycle send,compare: ![img 10][10] * single thread,same message size,use client channel pool or not,compare: ![img 11][11] ###consume * single thread,multiple message size,async receive,compare: ![img 12][12] * single thread,same message size,use client channel pool or not,compare: ![img 13][13] ##licence Copyright (c) 2014-2015 yanghua. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at ``` http://www.apache.org/licenses/LICENSE-2.0 ``` Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. [1]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/overview/architecture.png [2]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/overview/module-dependency.png [3]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/overview/router-topology.png [4]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/client/carry-inherits.png [5]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/client/handle-chain.png [6]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/client/handler-chain-config.png [7]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/client/node.png [8]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/client/node-db-info.png [9]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/common/message-design.png [10]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/benchmark/produce/singleThreadClientVSOriginal.png [11]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/benchmark/produce/singleThreadOptionPool.png [12]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/benchmark/consume/singleThreadClientVSOriginal.png [13]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/benchmark/consume/singleThreadOptionPool.png [14]:https://raw.githubusercontent.com/yanghua/banyan/master/screenshots/overview/rabbitmq-offical-screenshot.png [15]:https://raw.githubusercontent.com/yanghua/banyan/master/screenshots/overview/banyan.jpg [16]:https://raw.githubusercontent.com/yanghua/banyan/master/screenshots/overview/webconsole-dashboard.png [17]:https://raw.githubusercontent.com/yanghua/banyan/master/screenshots/overview/webconsole-queueManage.png ================================================ FILE: benchmark/README-CN.md ================================================ #overview 本项目用于对消息总线的 `client` 进行测试,以展示不同场景下 `client` 的性能表现。 ##测试说明 配置说明: client : ``` OS : Mac os x Yosemite (version 10.10) Processor : 2.5GHz Intel Core i5 Memory : 8GB 1600 MHz DDR3 JDK Version : 1.7.0_45 ``` server : ``` OS : Ubuntu Server 14.04.1 (GNU/Linux 3.13.0-37-generic x86_64) Processor : Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz (8核) Memory : 8GB JDK Version : 1.7.0_72 ``` > 每次测试之前,rabbitmq都会重新初始化,也就是说,每次测试之前总线中没有消息! ##实现说明 测试模块的相关类图: ![img 1][1] 测试逻辑由一个个测试用例构成,测试用例都是形如XXXTestCase的Java文件,所有的TestCase都继承自 `Benchmark` 类,它提供了一个 `test` 方法: ``` public void test(Runnable testTask, int holdTime, int fetchNum, String fileName); ``` 它需要的几个参数: * testTask - 具体的测试逻辑所属类,上面的 `BasicProduce`的实例 * holdTime - 测试维持的毫秒数 * fetchNum - 采样次数 * fileName - 采集数据要存储的文件名 `test` 方法提供了一个实现,并完成了一些 `Aspect`,它们有: * 日志记录 * testTask的多线程启动 * 测试数据的采集 * testTask的关闭 * 采集数据写入文件 每个继承自 `Benchmark` 的 testcase 都包含有若干个 testTask (它们都是静态内部类,都实现了 `Runnable`接口,如上图的 `BasicProduce`),每个testcase都包含有一个 `main` 入口方法,该方法用于运行测试。 对于每一个testTask,它们通常都会实现两个接口: * ITerminater : 终止testTask的接口 * IFetcher : 提取/采集测试数据的接口 这两个接口在 `Benchmark` 的 `test` 方法中使用。 除此之外,还有几个辅助类: - TestMessageFactory : 用于安装给定的size来生成测试消息 - TestConfigConstant : 用于统一配置一些测试常量 - TestUtility : 帮助类,测试数据写文件等 ##scenario ###produce > orignal : native-java-client 发送,但也走server的topology route,测试时共享一个channel > client : 如无特殊说明,client发送时都基于pool-channel模式 * 单线程,不同大小的消息体,循环发送,对比: ![img 2][2] * 单线程,相同大小的消息体,是否使用client channel pool,对比: ![img 3][3] ###consume >所有消息以接收后拆包封装为 `Message` 对象,为接收完成,所有的测试都基于rabbitmq server中预先生成 **50W** 条消息为基础 * 单线程,不同大小的消息体,异步接收,对比: ![img 4][4] * 单线程,相同大小的消息体,是否使用client channel pool,对比: ![img 5][5] >由于在异步消费的时候,本身就是使用单个channel的长连接事件循环,所以此处基本没有体现出差异。 ###其他通信方式 由于其他两种消息通信方式都是由produce/consume演变而来,因此其他场景下的 **单方面** 的性能跟以上相当! [1]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/benchmark/benchmark-class-diagram.png [2]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/benchmark/produce/singleThreadClientVSOriginal.png [3]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/benchmark/produce/singleThreadOptionPool.png [4]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/benchmark/consume/singleThreadClientVSOriginal.png [5]:https://raw.githubusercontent.com/yanghua/messagebus/master/screenshots/benchmark/consume/singleThreadOptionPool.png ================================================ FILE: benchmark/pom.xml ================================================ messagebus com.messagebus 1.0.5 4.0.0 benchmark com.messagebus client 1.0.5 org.apache.thrift libthrift 0.9.2 org.slf4j slf4j-api 1.7.12 ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/Benchmark.java ================================================ package com.messagebus.benchmark.client; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; public abstract class Benchmark { private static final Log logger = LogFactory.getLog(Benchmark.class); public void test(Runnable testTask, int holdTime, int fetchNum, String fileName) { logger.info("test begin"); int fetchInterval = holdTime / fetchNum; long[] xArr = new long[fetchNum]; long[] yArr = new long[fetchNum]; //initialize value xArr[0] = 0; yArr[0] = 0; ILifeCycle testCase = (ILifeCycle) testTask; testCase.start(); try { for (int i = 1; i < fetchNum; i++) { TimeUnit.MILLISECONDS.sleep(fetchInterval); xArr[i] = i * fetchInterval; yArr[i] = ((IFetcher) testTask).fetch(); } ((ILifeCycle) testTask).terminate(); //write to report file TestUtility.writeFile(fileName, xArr, yArr); } catch (InterruptedException e) { } logger.info("test end"); } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/IFetcher.java ================================================ package com.messagebus.benchmark.client; public interface IFetcher { long fetch(); } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/ILifeCycle.java ================================================ package com.messagebus.benchmark.client; public interface ILifeCycle { void start(); void terminate(); } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/TestConfigConstant.java ================================================ package com.messagebus.benchmark.client; public class TestConfigConstant { public static final String ORIGINAL_PRODUCE_ROUTING_KEY = "routingkey.proxy.message.procon.erpDemoConsume"; public static final String DEFAULT_EXCHANGE_NAME_WITHOUT_TOPOLOGY = "amq.fanout"; public static final String PRODUCER_SECRET = "kljasdoifqoikjhhhqwhebasdfasdf"; public static final String PRODUCER_TOKEN = "hlkasjdhfkqlwhlfalksjdhgssssas"; public static final String CONSUMER_QUEUE_NAME = "emapDemoConsume"; public static final String CONSUMER_SECRET = "zxdjnflakwenklasjdflkqpiasdfnj"; public static final String OUTPUT_FILE_PATH_FORMAT = "/tmp/%s.data"; public static final int MSG_BODY_SIZE_OF_BYTE = 3000; //B public static final int HOLD_TIME_OF_MILLIS = 60000; //one minute public static final int FETCH_NUM = 6; } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/TestMessageFactory.java ================================================ package com.messagebus.benchmark.client; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.client.message.model.MessageType; public class TestMessageFactory { public static Message create(MessageType type, int sizeOfByte) { Message msg = MessageFactory.createMessage(type); byte[] content = generate(sizeOfByte); msg.setContent(content); return msg; } public static Message[] create(MessageType type, int sizeOfByte, int num) { Message[] msgs = new Message[num]; Message msg = create(type, sizeOfByte); for (int i = 0; i < num; i++) { msgs[i] = msg; } return msgs; } private static byte[] generate(int sizeOfByte) { byte[] result = new byte[sizeOfByte]; for (int i = 0; i < sizeOfByte; i++) { result[i] = (byte) 1; } return result; } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/TestUtility.java ================================================ package com.messagebus.benchmark.client; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.MessagebusUnOpenException; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageType; import com.messagebus.common.ExceptionHelper; import com.messagebus.common.TestVariableInfo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.*; public class TestUtility { private final static Log logger = LogFactory.getLog(TestUtility.class); public static void writeFile(String fileName, long[] xArr, long[] yArr) { String filePath = String.format(TestConfigConstant.OUTPUT_FILE_PATH_FORMAT, fileName); File dataFile = new File(filePath); try { if (!dataFile.exists() && (!dataFile.createNewFile())) { throw new RuntimeException("create new file at : " + filePath + " , failure."); } } catch (IOException e) { ExceptionHelper.logException(logger, e, "writeFile"); throw new RuntimeException(e); } FileWriter fileWriter = null; PrintWriter out = null; try { fileWriter = new FileWriter(filePath); out = new PrintWriter(fileWriter); out.println("#x y"); for (int i = 0; i < xArr.length; i++) { out.println(xArr[i] + " " + yArr[i]); } out.flush(); } catch (IOException e) { ExceptionHelper.logException(logger, e, "writeFile"); throw new RuntimeException(e); } finally { try { if (fileWriter != null) fileWriter.close(); if (out != null) out.close(); } catch (IOException e) { } } } public static void exec(String[] cmds, boolean hasOutput) { if (cmds == null || cmds.length == 0) { return; } if (hasOutput) { Process process = null; try { process = Runtime.getRuntime().exec(cmds); } catch (IOException e) { ExceptionHelper.logException(logger, e, "exec"); throw new RuntimeException(e.toString()); } InputStreamReader ir = null; LineNumberReader input = null; try { ir = new InputStreamReader(process.getInputStream()); input = new LineNumberReader(ir); String line; while ((line = input.readLine()) != null) System.out.println(line); } catch (IOException e) { ExceptionHelper.logException(logger, e, "exec"); throw new RuntimeException(e.toString()); } finally { try { if (ir != null) ir.close(); if (input != null) input.close(); } catch (IOException e) { } } } } public static void produce(long total) { Message msg = TestMessageFactory.create(MessageType.QueueMessage, TestConfigConstant.MSG_BODY_SIZE_OF_BYTE); MessagebusSinglePool singlePool = new MessagebusSinglePool(TestVariableInfo.PUBSUBER_HOST, TestVariableInfo.PUBSUBER_PORT); Messagebus client = singlePool.getResource(); try { for (int i = 0; i < total; i++) { client.produce(TestConfigConstant.PRODUCER_SECRET, TestConfigConstant.CONSUMER_QUEUE_NAME, msg, TestConfigConstant.PRODUCER_TOKEN); } } catch (MessagebusUnOpenException e) { e.printStackTrace(); } finally { singlePool.returnResource(client); singlePool.destroy(); } } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/CalcService.java ================================================ /** * Autogenerated by Thrift Compiler (0.9.2) * * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING * @generated */ package com.messagebus.benchmark.client.testCase; import org.apache.thrift.EncodingUtils; import org.apache.thrift.TException; import org.apache.thrift.async.AsyncMethodCallback; import org.apache.thrift.protocol.TTupleProtocol; import org.apache.thrift.scheme.IScheme; import org.apache.thrift.scheme.SchemeFactory; import org.apache.thrift.scheme.StandardScheme; import org.apache.thrift.scheme.TupleScheme; import org.apache.thrift.server.AbstractNonblockingServer.AsyncFrameBuffer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Generated; import java.util.*; @SuppressWarnings({"cast", "rawtypes", "serial", "unchecked"}) @Generated(value = "Autogenerated by Thrift Compiler (0.9.2)", date = "2015-4-17") public class CalcService { public interface Iface { public int calcSum() throws TException; } public interface AsyncIface { public void calcSum(AsyncMethodCallback resultHandler) throws TException; } public static class Client extends org.apache.thrift.TServiceClient implements Iface { public static class Factory implements org.apache.thrift.TServiceClientFactory { public Factory() { } public Client getClient(org.apache.thrift.protocol.TProtocol prot) { return new Client(prot); } public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) { return new Client(iprot, oprot); } } public Client(org.apache.thrift.protocol.TProtocol prot) { super(prot, prot); } public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) { super(iprot, oprot); } public int calcSum() throws TException { send_calcSum(); return recv_calcSum(); } public void send_calcSum() throws TException { calcSum_args args = new calcSum_args(); sendBase("calcSum", args); } public int recv_calcSum() throws TException { calcSum_result result = new calcSum_result(); receiveBase(result, "calcSum"); if (result.isSetSuccess()) { return result.success; } throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, "calcSum failed: unknown result"); } } public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface { public static class Factory implements org.apache.thrift.async.TAsyncClientFactory { private org.apache.thrift.async.TAsyncClientManager clientManager; private org.apache.thrift.protocol.TProtocolFactory protocolFactory; public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) { this.clientManager = clientManager; this.protocolFactory = protocolFactory; } public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) { return new AsyncClient(protocolFactory, clientManager, transport); } } public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) { super(protocolFactory, clientManager, transport); } public void calcSum(AsyncMethodCallback resultHandler) throws TException { checkReady(); calcSum_call method_call = new calcSum_call(resultHandler, this, ___protocolFactory, ___transport); this.___currentMethod = method_call; ___manager.call(method_call); } public static class calcSum_call extends org.apache.thrift.async.TAsyncMethodCall { public calcSum_call(AsyncMethodCallback resultHandler, org.apache.thrift.async.TAsyncClient client, org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.transport.TNonblockingTransport transport) throws TException { super(client, protocolFactory, transport, resultHandler, false); } public void write_args(org.apache.thrift.protocol.TProtocol prot) throws TException { prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage("calcSum", org.apache.thrift.protocol.TMessageType.CALL, 0)); calcSum_args args = new calcSum_args(); args.write(prot); prot.writeMessageEnd(); } public int getResult() throws TException { if (getState() != State.RESPONSE_READ) { throw new IllegalStateException("Method call not finished!"); } org.apache.thrift.transport.TMemoryInputTransport memoryTransport = new org.apache.thrift.transport.TMemoryInputTransport(getFrameBuffer().array()); org.apache.thrift.protocol.TProtocol prot = client.getProtocolFactory().getProtocol(memoryTransport); return (new Client(prot)).recv_calcSum(); } } } public static class Processor extends org.apache.thrift.TBaseProcessor implements org.apache.thrift.TProcessor { private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName()); public Processor(I iface) { super(iface, getProcessMap(new HashMap>())); } protected Processor(I iface, Map> processMap) { super(iface, getProcessMap(processMap)); } private static Map> getProcessMap(Map> processMap) { processMap.put("calcSum", new calcSum()); return processMap; } public static class calcSum extends org.apache.thrift.ProcessFunction { public calcSum() { super("calcSum"); } public calcSum_args getEmptyArgsInstance() { return new calcSum_args(); } protected boolean isOneway() { return false; } public calcSum_result getResult(I iface, calcSum_args args) throws TException { calcSum_result result = new calcSum_result(); result.success = iface.calcSum(); result.setSuccessIsSet(true); return result; } } } public static class AsyncProcessor extends org.apache.thrift.TBaseAsyncProcessor { private static final Logger LOGGER = LoggerFactory.getLogger(AsyncProcessor.class.getName()); public AsyncProcessor(I iface) { super(iface, getProcessMap(new HashMap>())); } protected AsyncProcessor(I iface, Map> processMap) { super(iface, getProcessMap(processMap)); } private static Map> getProcessMap(Map> processMap) { processMap.put("calcSum", new calcSum()); return processMap; } public static class calcSum extends org.apache.thrift.AsyncProcessFunction { public calcSum() { super("calcSum"); } public calcSum_args getEmptyArgsInstance() { return new calcSum_args(); } public AsyncMethodCallback getResultHandler(final AsyncFrameBuffer fb, final int seqid) { final org.apache.thrift.AsyncProcessFunction fcall = this; return new AsyncMethodCallback() { public void onComplete(Integer o) { calcSum_result result = new calcSum_result(); result.success = o; result.setSuccessIsSet(true); try { fcall.sendResponse(fb, result, org.apache.thrift.protocol.TMessageType.REPLY, seqid); return; } catch (Exception e) { LOGGER.error("Exception writing to internal frame buffer", e); } fb.close(); } public void onError(Exception e) { byte msgType = org.apache.thrift.protocol.TMessageType.REPLY; org.apache.thrift.TBase msg; calcSum_result result = new calcSum_result(); { msgType = org.apache.thrift.protocol.TMessageType.EXCEPTION; msg = (org.apache.thrift.TBase) new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.INTERNAL_ERROR, e.getMessage()); } try { fcall.sendResponse(fb, msg, msgType, seqid); return; } catch (Exception ex) { LOGGER.error("Exception writing to internal frame buffer", ex); } fb.close(); } }; } protected boolean isOneway() { return false; } public void start(I iface, calcSum_args args, AsyncMethodCallback resultHandler) throws TException { iface.calcSum(resultHandler); } } } public static class calcSum_args implements org.apache.thrift.TBase, java.io.Serializable, Cloneable, Comparable { private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("calcSum_args"); private static final Map, SchemeFactory> schemes = new HashMap, SchemeFactory>(); static { schemes.put(StandardScheme.class, new calcSum_argsStandardSchemeFactory()); schemes.put(TupleScheme.class, new calcSum_argsTupleSchemeFactory()); } /** * The set of fields this struct contains, along with convenience methods for finding and manipulating them. */ public enum _Fields implements org.apache.thrift.TFieldIdEnum { ; private static final Map byName = new HashMap(); static { for (_Fields field : EnumSet.allOf(_Fields.class)) { byName.put(field.getFieldName(), field); } } /** * Find the _Fields constant that matches fieldId, or null if its not found. */ public static _Fields findByThriftId(int fieldId) { switch (fieldId) { default: return null; } } /** * Find the _Fields constant that matches fieldId, throwing an exception * if it is not found. */ public static _Fields findByThriftIdOrThrow(int fieldId) { _Fields fields = findByThriftId(fieldId); if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!"); return fields; } /** * Find the _Fields constant that matches name, or null if its not found. */ public static _Fields findByName(String name) { return byName.get(name); } private final short _thriftId; private final String _fieldName; _Fields(short thriftId, String fieldName) { _thriftId = thriftId; _fieldName = fieldName; } public short getThriftFieldId() { return _thriftId; } public String getFieldName() { return _fieldName; } } public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap; static { Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class); metaDataMap = Collections.unmodifiableMap(tmpMap); org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(calcSum_args.class, metaDataMap); } public calcSum_args() { } /** * Performs a deep copy on other. */ public calcSum_args(calcSum_args other) { } public calcSum_args deepCopy() { return new calcSum_args(this); } @Override public void clear() { } public void setFieldValue(_Fields field, Object value) { switch (field) { } } public Object getFieldValue(_Fields field) { switch (field) { } throw new IllegalStateException(); } /** * Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */ public boolean isSet(_Fields field) { if (field == null) { throw new IllegalArgumentException(); } switch (field) { } throw new IllegalStateException(); } @Override public boolean equals(Object that) { if (that == null) return false; if (that instanceof calcSum_args) return this.equals((calcSum_args) that); return false; } public boolean equals(calcSum_args that) { if (that == null) return false; return true; } @Override public int hashCode() { List list = new ArrayList(); return list.hashCode(); } @Override public int compareTo(calcSum_args other) { if (!getClass().equals(other.getClass())) { return getClass().getName().compareTo(other.getClass().getName()); } int lastComparison = 0; return 0; } public _Fields fieldForId(int fieldId) { return _Fields.findByThriftId(fieldId); } public void read(org.apache.thrift.protocol.TProtocol iprot) throws TException { schemes.get(iprot.getScheme()).getScheme().read(iprot, this); } public void write(org.apache.thrift.protocol.TProtocol oprot) throws TException { schemes.get(oprot.getScheme()).getScheme().write(oprot, this); } @Override public String toString() { StringBuilder sb = new StringBuilder("calcSum_args("); boolean first = true; sb.append(")"); return sb.toString(); } public void validate() throws TException { // check for required fields // check for sub-struct validity } private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { try { write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out))); } catch (TException te) { throw new java.io.IOException(te); } } private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { try { read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in))); } catch (TException te) { throw new java.io.IOException(te); } } private static class calcSum_argsStandardSchemeFactory implements SchemeFactory { public calcSum_argsStandardScheme getScheme() { return new calcSum_argsStandardScheme(); } } private static class calcSum_argsStandardScheme extends StandardScheme { public void read(org.apache.thrift.protocol.TProtocol iprot, calcSum_args struct) throws TException { org.apache.thrift.protocol.TField schemeField; iprot.readStructBegin(); while (true) { schemeField = iprot.readFieldBegin(); if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { break; } switch (schemeField.id) { default: org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } iprot.readFieldEnd(); } iprot.readStructEnd(); // check for required fields of primitive type, which can't be checked in the validate method struct.validate(); } public void write(org.apache.thrift.protocol.TProtocol oprot, calcSum_args struct) throws TException { struct.validate(); oprot.writeStructBegin(STRUCT_DESC); oprot.writeFieldStop(); oprot.writeStructEnd(); } } private static class calcSum_argsTupleSchemeFactory implements SchemeFactory { public calcSum_argsTupleScheme getScheme() { return new calcSum_argsTupleScheme(); } } private static class calcSum_argsTupleScheme extends TupleScheme { @Override public void write(org.apache.thrift.protocol.TProtocol prot, calcSum_args struct) throws TException { TTupleProtocol oprot = (TTupleProtocol) prot; } @Override public void read(org.apache.thrift.protocol.TProtocol prot, calcSum_args struct) throws TException { TTupleProtocol iprot = (TTupleProtocol) prot; } } } public static class calcSum_result implements org.apache.thrift.TBase, java.io.Serializable, Cloneable, Comparable { private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("calcSum_result"); private static final org.apache.thrift.protocol.TField SUCCESS_FIELD_DESC = new org.apache.thrift.protocol.TField("success", org.apache.thrift.protocol.TType.I32, (short) 0); private static final Map, SchemeFactory> schemes = new HashMap, SchemeFactory>(); static { schemes.put(StandardScheme.class, new calcSum_resultStandardSchemeFactory()); schemes.put(TupleScheme.class, new calcSum_resultTupleSchemeFactory()); } public int success; // required /** * The set of fields this struct contains, along with convenience methods for finding and manipulating them. */ public enum _Fields implements org.apache.thrift.TFieldIdEnum { SUCCESS((short) 0, "success"); private static final Map byName = new HashMap(); static { for (_Fields field : EnumSet.allOf(_Fields.class)) { byName.put(field.getFieldName(), field); } } /** * Find the _Fields constant that matches fieldId, or null if its not found. */ public static _Fields findByThriftId(int fieldId) { switch (fieldId) { case 0: // SUCCESS return SUCCESS; default: return null; } } /** * Find the _Fields constant that matches fieldId, throwing an exception * if it is not found. */ public static _Fields findByThriftIdOrThrow(int fieldId) { _Fields fields = findByThriftId(fieldId); if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!"); return fields; } /** * Find the _Fields constant that matches name, or null if its not found. */ public static _Fields findByName(String name) { return byName.get(name); } private final short _thriftId; private final String _fieldName; _Fields(short thriftId, String fieldName) { _thriftId = thriftId; _fieldName = fieldName; } public short getThriftFieldId() { return _thriftId; } public String getFieldName() { return _fieldName; } } // isset id assignments private static final int __SUCCESS_ISSET_ID = 0; private byte __isset_bitfield = 0; public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap; static { Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class); tmpMap.put(_Fields.SUCCESS, new org.apache.thrift.meta_data.FieldMetaData("success", org.apache.thrift.TFieldRequirementType.DEFAULT, new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.I32))); metaDataMap = Collections.unmodifiableMap(tmpMap); org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(calcSum_result.class, metaDataMap); } public calcSum_result() { } public calcSum_result( int success) { this(); this.success = success; setSuccessIsSet(true); } /** * Performs a deep copy on other. */ public calcSum_result(calcSum_result other) { __isset_bitfield = other.__isset_bitfield; this.success = other.success; } public calcSum_result deepCopy() { return new calcSum_result(this); } @Override public void clear() { setSuccessIsSet(false); this.success = 0; } public int getSuccess() { return this.success; } public calcSum_result setSuccess(int success) { this.success = success; setSuccessIsSet(true); return this; } public void unsetSuccess() { __isset_bitfield = EncodingUtils.clearBit(__isset_bitfield, __SUCCESS_ISSET_ID); } /** * Returns true if field success is set (has been assigned a value) and false otherwise */ public boolean isSetSuccess() { return EncodingUtils.testBit(__isset_bitfield, __SUCCESS_ISSET_ID); } public void setSuccessIsSet(boolean value) { __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __SUCCESS_ISSET_ID, value); } public void setFieldValue(_Fields field, Object value) { switch (field) { case SUCCESS: if (value == null) { unsetSuccess(); } else { setSuccess((Integer) value); } break; } } public Object getFieldValue(_Fields field) { switch (field) { case SUCCESS: return Integer.valueOf(getSuccess()); } throw new IllegalStateException(); } /** * Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */ public boolean isSet(_Fields field) { if (field == null) { throw new IllegalArgumentException(); } switch (field) { case SUCCESS: return isSetSuccess(); } throw new IllegalStateException(); } @Override public boolean equals(Object that) { if (that == null) return false; if (that instanceof calcSum_result) return this.equals((calcSum_result) that); return false; } public boolean equals(calcSum_result that) { if (that == null) return false; boolean this_present_success = true; boolean that_present_success = true; if (this_present_success || that_present_success) { if (!(this_present_success && that_present_success)) return false; if (this.success != that.success) return false; } return true; } @Override public int hashCode() { List list = new ArrayList(); boolean present_success = true; list.add(present_success); if (present_success) list.add(success); return list.hashCode(); } @Override public int compareTo(calcSum_result other) { if (!getClass().equals(other.getClass())) { return getClass().getName().compareTo(other.getClass().getName()); } int lastComparison = 0; lastComparison = Boolean.valueOf(isSetSuccess()).compareTo(other.isSetSuccess()); if (lastComparison != 0) { return lastComparison; } if (isSetSuccess()) { lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.success, other.success); if (lastComparison != 0) { return lastComparison; } } return 0; } public _Fields fieldForId(int fieldId) { return _Fields.findByThriftId(fieldId); } public void read(org.apache.thrift.protocol.TProtocol iprot) throws TException { schemes.get(iprot.getScheme()).getScheme().read(iprot, this); } public void write(org.apache.thrift.protocol.TProtocol oprot) throws TException { schemes.get(oprot.getScheme()).getScheme().write(oprot, this); } @Override public String toString() { StringBuilder sb = new StringBuilder("calcSum_result("); boolean first = true; sb.append("success:"); sb.append(this.success); first = false; sb.append(")"); return sb.toString(); } public void validate() throws TException { // check for required fields // check for sub-struct validity } private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { try { write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out))); } catch (TException te) { throw new java.io.IOException(te); } } private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { try { // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor. __isset_bitfield = 0; read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in))); } catch (TException te) { throw new java.io.IOException(te); } } private static class calcSum_resultStandardSchemeFactory implements SchemeFactory { public calcSum_resultStandardScheme getScheme() { return new calcSum_resultStandardScheme(); } } private static class calcSum_resultStandardScheme extends StandardScheme { public void read(org.apache.thrift.protocol.TProtocol iprot, calcSum_result struct) throws TException { org.apache.thrift.protocol.TField schemeField; iprot.readStructBegin(); while (true) { schemeField = iprot.readFieldBegin(); if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { break; } switch (schemeField.id) { case 0: // SUCCESS if (schemeField.type == org.apache.thrift.protocol.TType.I32) { struct.success = iprot.readI32(); struct.setSuccessIsSet(true); } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } break; default: org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } iprot.readFieldEnd(); } iprot.readStructEnd(); // check for required fields of primitive type, which can't be checked in the validate method struct.validate(); } public void write(org.apache.thrift.protocol.TProtocol oprot, calcSum_result struct) throws TException { struct.validate(); oprot.writeStructBegin(STRUCT_DESC); if (struct.isSetSuccess()) { oprot.writeFieldBegin(SUCCESS_FIELD_DESC); oprot.writeI32(struct.success); oprot.writeFieldEnd(); } oprot.writeFieldStop(); oprot.writeStructEnd(); } } private static class calcSum_resultTupleSchemeFactory implements SchemeFactory { public calcSum_resultTupleScheme getScheme() { return new calcSum_resultTupleScheme(); } } private static class calcSum_resultTupleScheme extends TupleScheme { @Override public void write(org.apache.thrift.protocol.TProtocol prot, calcSum_result struct) throws TException { TTupleProtocol oprot = (TTupleProtocol) prot; BitSet optionals = new BitSet(); if (struct.isSetSuccess()) { optionals.set(0); } oprot.writeBitSet(optionals, 1); if (struct.isSetSuccess()) { oprot.writeI32(struct.success); } } @Override public void read(org.apache.thrift.protocol.TProtocol prot, calcSum_result struct) throws TException { TTupleProtocol iprot = (TTupleProtocol) prot; BitSet incoming = iprot.readBitSet(1); if (incoming.get(0)) { struct.success = iprot.readI32(); struct.setSuccessIsSet(true); } } } } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/ConsumeTestCase.java ================================================ package com.messagebus.benchmark.client.testCase; import com.messagebus.benchmark.client.*; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.MessagebusUnOpenException; import com.messagebus.client.message.model.Message; import com.messagebus.common.TestVariableInfo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; public class ConsumeTestCase extends Benchmark { private static final Log logger = LogFactory.getLog(ConsumeTestCase.class); private static class BasicConsume implements Runnable, ILifeCycle, IFetcher { private MessagebusSinglePool singlePool; private Messagebus client; private long counter = 0; private Thread currentThread; private BasicConsume() { singlePool = new MessagebusSinglePool(TestVariableInfo.PUBSUBER_HOST, TestVariableInfo.PUBSUBER_PORT); client = singlePool.getResource(); currentThread = new Thread(this); currentThread.setDaemon(true); } @Override public long fetch() { return this.counter; } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { logger.info("closing test task ...."); this.currentThread.interrupt(); } @Override public void run() { try { client.consume(TestConfigConstant.CONSUMER_SECRET, Integer.MAX_VALUE, TimeUnit.SECONDS, new IMessageReceiveListener() { @Override public void onMessage(Message message) { ++counter; } }); } catch (MessagebusUnOpenException e) { e.printStackTrace(); } finally { singlePool.returnResource(client); singlePool.destroy(); } } } public static void main(String[] args) { // produce some message for consuming TestUtility.produce(500000); try { TimeUnit.SECONDS.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } ConsumeTestCase testCase = new ConsumeTestCase(); Runnable task = new BasicConsume(); testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, TestConfigConstant.FETCH_NUM, "single_thread_consume_async_" + TestConfigConstant.MSG_BODY_SIZE_OF_BYTE + "_KB"); } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/MessagebusJSONRpcTestCase.java ================================================ package com.messagebus.benchmark.client.testCase; import com.messagebus.benchmark.client.Benchmark; import com.messagebus.benchmark.client.IFetcher; import com.messagebus.benchmark.client.ILifeCycle; import com.messagebus.benchmark.client.TestConfigConstant; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.MessagebusUnOpenException; import com.messagebus.client.extension.thrift.TAMQPClientTransport; import com.messagebus.common.ExceptionHelper; import com.messagebus.common.TestVariableInfo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.thrift.TException; import org.apache.thrift.protocol.TJSONProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TTransport; /** * Created by yanghua on 4/17/15. */ public class MessagebusJSONRpcTestCase extends Benchmark { private static final Log logger = LogFactory.getLog(MessagebusJSONRpcTestCase.class); public static class OriginalJSONRpc implements Runnable, ILifeCycle, IFetcher { private MessagebusSinglePool singlePool; private Messagebus client; private boolean flag = true; private long counter = 0; private Thread currentThread; String secret = "kliwhiduhaiucvarkjajksdbfkjabw"; String targetQueue = "emapDemoRpcResponse"; String token = "klasehnfkljashdnflhkjahwlekdjf"; String methodName = "calcSum"; public OriginalJSONRpc() { singlePool = new MessagebusSinglePool(TestVariableInfo.PUBSUBER_HOST, TestVariableInfo.PUBSUBER_PORT); client = singlePool.getResource(); currentThread = new Thread(this); currentThread.setDaemon(true); } @Override public void run() { try { while (flag) { Object responseObj = client.call(secret, targetQueue, methodName, new Object[0], token, 10000); if (Integer.parseInt(responseObj.toString()) == 5050) ++counter; } } catch (MessagebusUnOpenException e) { ExceptionHelper.logException(logger, e, "[JSONRpc#run]"); } finally { singlePool.returnResource(client); singlePool.destroy(); } } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { logger.info("closing test task ...."); this.flag = false; } @Override public long fetch() { return this.counter; } } public static class ThriftJSONRpc implements Runnable, ILifeCycle, IFetcher { private MessagebusSinglePool singlePool; private Messagebus client; private Thread currentThread; private TTransport transport; private boolean flag = true; private long counter = 0; String secret = "kliwhiduhaiucvarkjajksdbfkjabw"; String targetQueue = "emapDemoRpcResponse"; String token = "klasehnfkljashdnflhkjahwlekdjf"; String methodName = "calcSum"; public ThriftJSONRpc() { singlePool = new MessagebusSinglePool(TestVariableInfo.PUBSUBER_HOST, TestVariableInfo.PUBSUBER_PORT); client = singlePool.getResource(); currentThread = new Thread(this); currentThread.setDaemon(true); } @Override public long fetch() { return this.counter; } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { logger.info("closing test task ...."); this.flag = false; } @Override public void run() { //client code transport = new TAMQPClientTransport(this.client, "kliwhiduhaiucvarkjajksdbfkjabw", "emapDemoRpcResponse", "klasehnfkljashdnflhkjahwlekdjf", 10000); int result = 0; try { transport.open(); TProtocol protocol = new TJSONProtocol(transport); CalcService.Client client = new CalcService.Client(protocol); while (flag) { result = client.calcSum(); if (result == 5050) ++counter; } } catch (TException e) { e.printStackTrace(); } finally { transport.close(); singlePool.returnResource(client); singlePool.destroy(); } } } public static void main(String[] args) { // MessagebusJSONRpcTestCase testCase = new MessagebusJSONRpcTestCase(); // Runnable task = new OriginalJSONRpc(); // // testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, // TestConfigConstant.FETCH_NUM, "messagebus_original_json_rpc_noHA"); MessagebusJSONRpcTestCase testCase = new MessagebusJSONRpcTestCase(); Runnable task = new ThriftJSONRpc(); testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, TestConfigConstant.FETCH_NUM, "messagebus_thrift_json_rpc_noHA"); } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/OriginalConsumeTestCase.java ================================================ package com.messagebus.benchmark.client.testCase; import com.messagebus.benchmark.client.Benchmark; import com.messagebus.benchmark.client.IFetcher; import com.messagebus.benchmark.client.ILifeCycle; import com.messagebus.benchmark.client.TestConfigConstant; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.client.message.model.MessageType; import com.messagebus.client.message.transfer.MessageHeaderTransfer; import com.messagebus.common.TestVariableInfo; import com.messagebus.interactor.proxy.ProxyConsumer; import com.messagebus.interactor.rabbitmq.AbstractInitializer; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.QueueingConsumer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; public class OriginalConsumeTestCase extends Benchmark { private static final Log logger = LogFactory.getLog(OriginalConsumeTestCase.class); public static class BasicConsume extends AbstractInitializer implements Runnable, ILifeCycle, IFetcher { private static final String consumerTag = "tag.consumer.msgLog"; private boolean flag = true; private long counter = 0; private String realQueueName = ""; private Thread currentThread; public BasicConsume(String host) { super(host); currentThread = new Thread(this); currentThread.setDaemon(true); } @Override public long fetch() { return this.counter; } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { this.flag = false; } @Override public void run() { try { super.init(); QueueingConsumer consumer = ProxyConsumer.consume(this.channel, this.getRealQueueName(), consumerTag); while (flag) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); AMQP.BasicProperties properties = delivery.getProperties(); byte[] msgBody = delivery.getBody(); this.channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); String msgTypeStr = properties.getType(); if (msgTypeStr == null || msgTypeStr.isEmpty()) { logger.error("[run] message type is null or empty"); } MessageType msgType = null; try { msgType = MessageType.lookup(msgTypeStr); } catch (UnknownError unknownError) { throw new RuntimeException("unknown message type : " + msgTypeStr); } Message msg = MessageFactory.createMessage(msgType); initMessage(msg, msgType, properties, msgBody); ++counter; } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { this.channel.basicCancel(consumerTag); super.close(); } catch (IOException e) { logger.error("[run] occurs a IOException : " + e.getMessage()); } } } private void initMessage(Message msg, MessageType msgType, AMQP.BasicProperties properties, byte[] bodyData) { MessageHeaderTransfer.unbox(properties, msg); msg.setContent(bodyData); } public String getRealQueueName() { return realQueueName; } public void setRealQueueName(String realQueueName) { this.realQueueName = realQueueName; } } public static void main(String[] args) { //produce some message for consuming // TestUtility.produce(50_0000); // // try { // TimeUnit.SECONDS.sleep(30); // } catch (InterruptedException e) { // e.printStackTrace(); // } OriginalConsumeTestCase testCase = new OriginalConsumeTestCase(); BasicConsume task = new BasicConsume(TestVariableInfo.RABBITMQ_SERVER_HOST); task.setRealQueueName("queue.proxy.message.business.crm"); testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, TestConfigConstant.FETCH_NUM, "single_thread_original_consume_async_" + TestConfigConstant.MSG_BODY_SIZE_OF_BYTE + "_KB"); } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/OriginalProduceTestCase.java ================================================ package com.messagebus.benchmark.client.testCase; import com.messagebus.benchmark.client.*; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageType; import com.messagebus.client.message.transfer.MessageHeaderTransfer; import com.messagebus.common.Constants; import com.messagebus.common.TestVariableInfo; import com.messagebus.interactor.proxy.ProxyProducer; import com.messagebus.interactor.rabbitmq.AbstractInitializer; import com.rabbitmq.client.AMQP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; public class OriginalProduceTestCase extends Benchmark { private static final Log logger = LogFactory.getLog(OriginalProduceTestCase.class); private static class BasicProduce extends AbstractInitializer implements Runnable, ILifeCycle, IFetcher { private Message msg; private String routingkey; private boolean flag = true; private long counter = 0; private Thread currentThread; private BasicProduce(String host, int msgBodySize) { super(host); msg = TestMessageFactory.create(MessageType.QueueMessage, msgBodySize); currentThread = new Thread(this); currentThread.setDaemon(true); } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { this.flag = false; } @Override public void run() { try { this.init(); AMQP.BasicProperties header = MessageHeaderTransfer.box(msg); while (flag) { ProxyProducer.produce(Constants.PROXY_EXCHANGE_NAME, this.channel, this.getRoutingkey(), msg.getContent(), header); ++counter; } } catch (IOException e) { e.printStackTrace(); } finally { try { this.close(); } catch (IOException e) { } } } @Override public long fetch() { return counter; } public String getRoutingkey() { return routingkey; } public void setRoutingkey(String routingkey) { this.routingkey = routingkey; } } public static void main(String[] args) { OriginalProduceTestCase testCase = new OriginalProduceTestCase(); BasicProduce task = new BasicProduce(TestVariableInfo.RABBITMQ_SERVER_HOST, TestConfigConstant.MSG_BODY_SIZE_OF_BYTE); task.setRoutingkey(TestConfigConstant.ORIGINAL_PRODUCE_ROUTING_KEY); testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, TestConfigConstant.FETCH_NUM, "one_thread_original_produce_one_by_one_" + TestConfigConstant.MSG_BODY_SIZE_OF_BYTE + "_Byte"); } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/OriginalProduceWithoutTopologyTestCase.java ================================================ package com.messagebus.benchmark.client.testCase; import com.messagebus.benchmark.client.*; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageType; import com.messagebus.client.message.transfer.MessageHeaderTransfer; import com.messagebus.common.TestVariableInfo; import com.messagebus.interactor.proxy.ProxyProducer; import com.messagebus.interactor.rabbitmq.AbstractInitializer; import com.rabbitmq.client.AMQP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; /** * Created by yanghua on 3/25/15. */ public class OriginalProduceWithoutTopologyTestCase extends Benchmark { private static final Log logger = LogFactory.getLog(OriginalProduceWithoutTopologyTestCase.class); private static class BasicProduce extends AbstractInitializer implements Runnable, ILifeCycle, IFetcher { private Message msg; private String routingkey; private boolean flag = true; private long counter = 0; private Thread currentThread; private BasicProduce(String host, int msgBodySize) { super(host); msg = TestMessageFactory.create(MessageType.QueueMessage, msgBodySize); currentThread = new Thread(this); currentThread.setDaemon(true); } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { this.flag = false; } @Override public void run() { try { this.init(); AMQP.BasicProperties header = MessageHeaderTransfer.box(msg); while (flag) { ProxyProducer.produce(TestConfigConstant.DEFAULT_EXCHANGE_NAME_WITHOUT_TOPOLOGY, this.channel, this.getRoutingkey(), msg.getContent(), header); ++counter; } } catch (IOException e) { e.printStackTrace(); } finally { try { this.close(); } catch (IOException e) { } } } @Override public long fetch() { return counter; } public String getRoutingkey() { return routingkey; } public void setRoutingkey(String routingkey) { this.routingkey = routingkey; } } public static void main(String[] args) { OriginalProduceTestCase testCase = new OriginalProduceTestCase(); BasicProduce task = new BasicProduce(TestVariableInfo.RABBITMQ_SERVER_HOST, TestConfigConstant.MSG_BODY_SIZE_OF_BYTE); task.setRoutingkey(""); testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, TestConfigConstant.FETCH_NUM, "one_thread_original_produce_without_topology_one_by_one_" + TestConfigConstant.MSG_BODY_SIZE_OF_BYTE + "_Byte"); } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/ProduceRemainLogTestCase.java ================================================ package com.messagebus.benchmark.client.testCase; import com.messagebus.benchmark.client.*; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.MessagebusUnOpenException; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageType; import com.messagebus.common.ExceptionHelper; import com.messagebus.common.TestVariableInfo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Created by yanghua on 3/25/15. */ public class ProduceRemainLogTestCase extends Benchmark { private static final Log logger = LogFactory.getLog(ProduceRemainLogTestCase.class); private static class BasicProduce implements Runnable, ILifeCycle, IFetcher { private MessagebusSinglePool singlePool; private Messagebus client; private Message msg; private boolean flag = true; private long counter = 0; private Thread currentThread; private BasicProduce(int msgBodySize) { msg = TestMessageFactory.create(MessageType.QueueMessage, msgBodySize); singlePool = new MessagebusSinglePool(TestVariableInfo.PUBSUBER_HOST, TestVariableInfo.PUBSUBER_PORT); client = singlePool.getResource(); currentThread = new Thread(this); currentThread.setDaemon(true); } @Override public void run() { try { while (flag) { client.produce(TestConfigConstant.PRODUCER_SECRET, TestConfigConstant.CONSUMER_QUEUE_NAME, msg, TestConfigConstant.PRODUCER_TOKEN); ++counter; } } catch (MessagebusUnOpenException e) { ExceptionHelper.logException(logger, e, "[BasicProduce#run]"); } finally { singlePool.returnResource(client); singlePool.destroy(); } } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { logger.info("closing test task ...."); this.flag = false; } @Override public long fetch() { return this.counter; } } public static void main(String[] args) { ProduceTestCase testCase = new ProduceTestCase(); Runnable task = new BasicProduce(TestConfigConstant.MSG_BODY_SIZE_OF_BYTE); testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, TestConfigConstant.FETCH_NUM, "one_thread_remain_log_produce_one_by_one_" + TestConfigConstant.MSG_BODY_SIZE_OF_BYTE + "_Byte"); } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/ProduceTestCase.java ================================================ package com.messagebus.benchmark.client.testCase; import com.messagebus.benchmark.client.*; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.MessagebusUnOpenException; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageType; import com.messagebus.common.ExceptionHelper; import com.messagebus.common.TestVariableInfo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ProduceTestCase extends Benchmark { private static final Log logger = LogFactory.getLog(ProduceTestCase.class); private static class BasicProduce implements Runnable, ILifeCycle, IFetcher { private MessagebusSinglePool singlePool; private Messagebus client; private Message msg; private boolean flag = true; private long counter = 0; private Thread currentThread; private BasicProduce(int msgBodySize) { msg = TestMessageFactory.create(MessageType.QueueMessage, msgBodySize); singlePool = new MessagebusSinglePool(TestVariableInfo.PUBSUBER_HOST, TestVariableInfo.PUBSUBER_PORT); client = singlePool.getResource(); currentThread = new Thread(this); currentThread.setDaemon(true); } @Override public void run() { try { while (flag) { client.produce(TestConfigConstant.PRODUCER_SECRET, TestConfigConstant.CONSUMER_QUEUE_NAME, msg, TestConfigConstant.PRODUCER_TOKEN); ++counter; } } catch (MessagebusUnOpenException e) { ExceptionHelper.logException(logger, e, "[BasicProduce#run]"); } finally { singlePool.returnResource(client); singlePool.destroy(); } } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { logger.info("closing test task ...."); this.flag = false; } @Override public long fetch() { return this.counter; } } public static void main(String[] args) { ProduceTestCase testCase = new ProduceTestCase(); Runnable task = new BasicProduce(TestConfigConstant.MSG_BODY_SIZE_OF_BYTE); testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, TestConfigConstant.FETCH_NUM, "one_thread_produce_one_by_one_" + TestConfigConstant.MSG_BODY_SIZE_OF_BYTE + "_Byte"); } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/ProduceWithoutLogTestCase.java ================================================ package com.messagebus.benchmark.client.testCase; import com.messagebus.benchmark.client.*; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.MessagebusUnOpenException; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageType; import com.messagebus.common.ExceptionHelper; import com.messagebus.common.TestVariableInfo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Created by yanghua on 3/25/15. */ public class ProduceWithoutLogTestCase extends Benchmark { private static final Log logger = LogFactory.getLog(ProduceWithoutLogTestCase.class); private static class BasicProduce implements Runnable, ILifeCycle, IFetcher { private MessagebusSinglePool singlePool; private Messagebus client; private Message msg; private boolean flag = true; private long counter = 0; private Thread currentThread; private BasicProduce(int msgBodySize) { msg = TestMessageFactory.create(MessageType.QueueMessage, msgBodySize); singlePool = new MessagebusSinglePool(TestVariableInfo.PUBSUBER_HOST, TestVariableInfo.PUBSUBER_PORT); client = singlePool.getResource(); currentThread = new Thread(this); currentThread.setDaemon(true); } @Override public void run() { try { while (flag) { client.produce(TestConfigConstant.PRODUCER_SECRET, TestConfigConstant.CONSUMER_QUEUE_NAME, msg, TestConfigConstant.PRODUCER_TOKEN); ++counter; } } catch (MessagebusUnOpenException e) { ExceptionHelper.logException(logger, e, "[BasicProduce#run]"); } finally { singlePool.returnResource(client); singlePool.destroy(); } } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { logger.info("closing test task ...."); this.flag = false; } @Override public long fetch() { return this.counter; } } public static void main(String[] args) { ProduceTestCase testCase = new ProduceTestCase(); Runnable task = new BasicProduce(TestConfigConstant.MSG_BODY_SIZE_OF_BYTE); testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, TestConfigConstant.FETCH_NUM, "one_thread_without_log_produce_one_by_one_" + TestConfigConstant.MSG_BODY_SIZE_OF_BYTE + "_Byte"); } } ================================================ FILE: benchmark/src/main/java/com/messagebus/benchmark/client/testCase/ThriftJSONRpcTestCase.java ================================================ package com.messagebus.benchmark.client.testCase; import com.messagebus.benchmark.client.Benchmark; import com.messagebus.benchmark.client.IFetcher; import com.messagebus.benchmark.client.ILifeCycle; import com.messagebus.benchmark.client.TestConfigConstant; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TJSONProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; /** * Created by yanghua on 4/17/15. */ public class ThriftJSONRpcTestCase extends Benchmark { private static final Log logger = LogFactory.getLog(ThriftJSONRpcTestCase.class); public static class JSONRpc implements Runnable, ILifeCycle, IFetcher { private Thread currentThread; private long counter = 0; private boolean flag = true; public JSONRpc() { this.currentThread = new Thread(this); this.currentThread.setDaemon(true); } @Override public long fetch() { return this.counter; } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { logger.info("closing test task ...."); this.flag = false; } @Override public void run() { TTransport transport = new TSocket("172.16.206.29", 7911); int result = 0; try { transport.open(); TProtocol protocol = new TJSONProtocol(transport); CalcService.Client client = new CalcService.Client(protocol); while (flag) { result = client.calcSum(); if (result == 5050) ++counter; } logger.info(result); } catch (TException e) { e.printStackTrace(); } finally { transport.close(); } } } public static class BinaryRpc implements Runnable, ILifeCycle, IFetcher { private Thread currentThread; private long counter = 0; private boolean flag = true; public BinaryRpc() { this.currentThread = new Thread(this); this.currentThread.setDaemon(true); } @Override public long fetch() { return this.counter; } @Override public void start() { this.currentThread.start(); } @Override public void terminate() { logger.info("closing test task ...."); this.flag = false; } @Override public void run() { TTransport transport = new TSocket("172.16.206.29", 7911); int result = 0; try { transport.open(); TProtocol protocol = new TBinaryProtocol(transport); CalcService.Client client = new CalcService.Client(protocol); while (flag) { result = client.calcSum(); if (result == 5050) ++counter; } } catch (TException e) { e.printStackTrace(); } finally { transport.close(); } } } public static void main(String[] args) { // ThriftJSONRpcTestCase testCase = new ThriftJSONRpcTestCase(); // // Runnable task = new JSONRpc(); // testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, // TestConfigConstant.FETCH_NUM, "thrift_original_json_rpc"); ThriftJSONRpcTestCase testCase = new ThriftJSONRpcTestCase(); Runnable task = new BinaryRpc(); testCase.test(task, TestConfigConstant.HOLD_TIME_OF_MILLIS, TestConfigConstant.FETCH_NUM, "thrift_original_binary_rpc"); } } ================================================ FILE: benchmark/src/main/resources/clearEnv.sh ================================================ #! /bin/sh sudo service messagebus-server stop sudo rabbitmqctl stop_app sudo rabbitmqctl reset sudo rabbitmqctl start_app sudo service messagebus-server start ================================================ FILE: benchmark/src/main/resources/log4j.properties ================================================ # Configure logging for testing log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.encoding=utf-8 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[%-5p] -%d{yyyy-MM-dd HH:mm:ss,SSS} method:%l - %m%n ================================================ FILE: benchmark/src/main/resources/plotscript_consume.sh ================================================ #! /usr/bin/env gnuplot reset set terminal png #write you image dir path set output "/Users/yanghua/Documents/GitHub/messagebus/screenshots/benchmark/consume/singleThreadClientVSOriginal.png" #set logscale x #set logscale y set xlabel "time (ms)" set ylabel "records num" set title "single thread async consume performance test " set key reverse Left outside set grid set autoscale set style data linespoints set yrange [0:500000] set xrange [0:60000] plot "/tmp/single_thread_consume_async_1.0_KB.data" using 1:2 title "client 1KB",\ "/tmp/single_thread_original_consume_async_1.0_KB.data" using 1:2 title "original 1KB",\ "/tmp/single_thread_consume_async_3.0_KB.data" using 1:2 title "client 3KB",\ "/tmp/single_thread_original_consume_async_3.0_KB.data" using 1:2 title "original 3KB",\ "/tmp/single_thread_consume_async_5.0_KB.data" using 1:2 title "client 5KB",\ "/tmp/single_thread_original_consume_async_5.0_KB.data" using 1:2 title "original 5KB"; ================================================ FILE: benchmark/src/main/resources/plotscript_messagebus_client_produce.sh ================================================ #! /usr/bin/env gnuplot reset set terminal png size 800,600 #write you image dir path set output "/Users/yanghua/Documents/GitHub/messagebus/screenshots/benchmark/produce/singleThreadMultipleMsgBodySizeMBClientProduce.png" #set logscale x #set logscale y set xlabel "time (ms)" set ylabel "records num" set title "single thread messagebus-client produce performance " set key reverse Left outside set grid set autoscale set style data linespoints set yrange [0:500000] set xrange [0:60000] plot "/tmp/one_thread_produce_one_by_one_500_Byte.data" using 1:2 title "messagebus 0.5KB",\ "/tmp/one_thread_produce_one_by_one_1000_Byte.data" using 1:2 title "messagebus 1KB",\ "/tmp/one_thread_produce_one_by_one_2000_Byte.data" using 1:2 title "messagebus 2KB",\ "/tmp/one_thread_produce_one_by_one_3000_Byte.data" using 1:2 title "messagebus 3KB"; ================================================ FILE: benchmark/src/main/resources/plotscript_messagebus_client_remain_log_produce.sh ================================================ #! /usr/bin/env gnuplot reset set terminal png size 800,600 #write you image dir path set output "/Users/yanghua/Documents/GitHub/messagebus/screenshots/benchmark/produce/singleThreadMultipleMsgBodySizeMBClientRemainLogProduce.png" #set logscale x #set logscale y set xlabel "time (ms)" set ylabel "records num" set title "one thread mb-client remain log produce performance " set key reverse Left outside set grid set autoscale set style data linespoints set yrange [0:500000] set xrange [0:60000] plot "/tmp/one_thread_remain_log_produce_one_by_one_500_Byte.data" using 1:2 title "messagebus 0.5KB",\ "/tmp/one_thread_remain_log_produce_one_by_one_1000_Byte.data" using 1:2 title "messagebus 1KB",\ "/tmp/one_thread_remain_log_produce_one_by_one_2000_Byte.data" using 1:2 title "messagebus 2KB",\ "/tmp/one_thread_remain_log_produce_one_by_one_3000_Byte.data" using 1:2 title "messagebus 3KB"; ================================================ FILE: benchmark/src/main/resources/plotscript_messagebus_client_without_realtime_log_produce.sh ================================================ #! /usr/bin/env gnuplot reset set terminal png size 800,600 #write you image dir path set output "/Users/yanghua/Documents/GitHub/messagebus/screenshots/benchmark/produce/singleThreadMultipleMsgBodySizeMBClientWithoutRealtimeLogProduce.png" #set logscale x #set logscale y set xlabel "time (ms)" set ylabel "records num" set title "one thread mb-client no realtime log produce performance " set key reverse Left outside set grid set autoscale set style data linespoints set yrange [0:500000] set xrange [0:60000] plot "/tmp/one_thread_without_log_produce_one_by_one_500_Byte.data" using 1:2 title "messagebus 0.5KB",\ "/tmp/one_thread_without_log_produce_one_by_one_1000_Byte.data" using 1:2 title "messagebus 1KB",\ "/tmp/one_thread_without_log_produce_one_by_one_2000_Byte.data" using 1:2 title "messagebus 2KB",\ "/tmp/one_thread_without_log_produce_one_by_one_3000_Byte.data" using 1:2 title "messagebus 3KB"; ================================================ FILE: benchmark/src/main/resources/plotscript_official_client_produce.sh ================================================ #! /usr/bin/env gnuplot reset set terminal png size 800,600 #write you image dir path set output "/Users/yanghua/Documents/GitHub/messagebus/screenshots/benchmark/produce/singleThreadMultipleMsgBodySizeOfficialClientProduce.png" #set logscale x #set logscale y set xlabel "time (ms)" set ylabel "records num" set title "single thread official-client produce performance " set key reverse Left outside set grid set autoscale set style data linespoints set yrange [0:500000] set xrange [0:60000] plot "/tmp/one_thread_original_produce_one_by_one_500_Byte.data" using 1:2 title "official 0.5KB",\ "/tmp/one_thread_original_produce_one_by_one_1000_Byte.data" using 1:2 title "official 1KB",\ "/tmp/one_thread_original_produce_one_by_one_2000_Byte.data" using 1:2 title "official 2KB",\ "/tmp/one_thread_original_produce_one_by_one_3000_Byte.data" using 1:2 title "official 3KB"; ================================================ FILE: benchmark/src/main/resources/plotscript_official_client_without_topology_produce.sh ================================================ #! /usr/bin/env gnuplot reset set terminal png size 800,600 #write you image dir path set output "/Users/yanghua/Documents/GitHub/messagebus/screenshots/benchmark/produce/singleThreadMultipleMsgBodySizeOfficialClientWithoutTopologyProduce.png" #set logscale x #set logscale y set xlabel "time (ms)" set ylabel "records num" set title "single thread official-client without topology produce performance " set key reverse Left outside set grid set autoscale set style data linespoints set yrange [0:500000] set xrange [0:60000] plot "/tmp/one_thread_original_produce_without_topology_one_by_one_500_Byte.data" using 1:2 title "official 0.5KB",\ "/tmp/one_thread_original_produce_without_topology_one_by_one_1000_Byte.data" using 1:2 title "official 1KB",\ "/tmp/one_thread_original_produce_without_topology_one_by_one_2000_Byte.data" using 1:2 title "official 2KB",\ "/tmp/one_thread_original_produce_without_topology_one_by_one_3000_Byte.data" using 1:2 title "official 3KB"; ================================================ FILE: client/pom.xml ================================================ messagebus com.messagebus 1.0.5 4.0.0 client jar com.messagebus common 1.0.5 com.messagebus interactor-component 1.0.5 org.apache.curator curator-framework 2.6.0 org.apache.curator curator-recipes 2.6.0 org.apache.commons commons-pool2 2.2 jar compile org.apache.thrift libthrift 0.9.2 messagebus-client org.apache.maven.plugins maven-jar-plugin 2.4 true true maven-compiler-plugin 2.3.2 true 1.6 1.6 UTF-8 org.apache.maven.plugins maven-surefire-plugin true org.apache.maven.plugins maven-dependency-plugin copy install copy-dependencies ${project.build.directory} maven-assembly-plugin 2.2-beta-5 jar-with-dependencies release release@172.16.1.104 http://172.16.1.104:8081/nexus/content/repositories/releases/ ================================================ FILE: client/src/main/java/com/messagebus/client/AbstractPool.java ================================================ package com.messagebus.client; import com.messagebus.common.ExceptionHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; /** * the abstract pool * * @param the Object that the pool want to cache */ abstract class AbstractPool { private static final Log logger = LogFactory.getLog(AbstractPool.class); protected GenericObjectPool internalPool; public AbstractPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory factory) { this.initPool(poolConfig, factory); } private void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory factory) { if (this.internalPool != null) { closeInternalPool(); } this.internalPool = new GenericObjectPool(factory, poolConfig); } public T getResource() { try { return internalPool.borrowObject(); } catch (Exception e) { ExceptionHelper.logException(logger, e, "[getResource]"); throw new RuntimeException("can not get a resource from the pool ", e); } } public void returnResourceObject(final T resource) { internalPool.returnObject(resource); } public void returnBrokenResource(final T resource) { returnBrokenResourceObject(resource); } public void returnResource(final T resource) { returnResourceObject(resource); } public void destroy() { closeInternalPool(); } protected void returnBrokenResourceObject(final T resource) { try { internalPool.invalidateObject(resource); } catch (Exception e) { throw new RuntimeException("Could not return the resource to the pool", e); } } protected void closeInternalPool() { internalPool.close(); } } ================================================ FILE: client/src/main/java/com/messagebus/client/Address.java ================================================ package com.messagebus.client; import java.io.Serializable; /** * Created by yanghua on 2/12/2015. */ public class Address implements Serializable { private String host; private int port; public Address() { } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } } ================================================ FILE: client/src/main/java/com/messagebus/client/ConfigManager.java ================================================ package com.messagebus.client; import com.google.common.base.Strings; import com.google.common.eventbus.EventBus; import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.PathChildrenCache; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; import java.util.HashMap; import java.util.Map; /** * the config manager */ public class ConfigManager { private static final Log logger = LogFactory.getLog(ConfigManager.class); private static final Gson GSON = new Gson(); private static final String SOURCE_EVENT_TYPE = "source"; private static final String SINK_EVENT_TYPE = "sink"; private static final String STREAM_EVENT_TYPE = "stream"; private static final String REVERSE_MESSAGE_ZK_PATH = "/reverse/message"; private static final String REVERSE_MESSAGE_SOURCE_ZK_PATH = REVERSE_MESSAGE_ZK_PATH + "/source"; private static final String REVERSE_MESSAGE_SINK_ZK_PATH = REVERSE_MESSAGE_ZK_PATH + "/sink"; private static final String REVERSE_MESSAGE_STREAM_ZK_PATH = REVERSE_MESSAGE_ZK_PATH + "/stream"; private static final String REVERSE_MESSAGE_SOURCE_SECRET_ZK_PATH = REVERSE_MESSAGE_SOURCE_ZK_PATH + "/secret"; private static final String REVERSE_MESSAGE_SOURCE_NAME_ZK_PATH = REVERSE_MESSAGE_SOURCE_ZK_PATH + "/name"; private static final String REVERSE_MESSAGE_SINK_SECRET_ZK_PATH = REVERSE_MESSAGE_SINK_ZK_PATH + "/secret"; private static final String REVERSE_MESSAGE_SINK_NAME_ZK_PATH = REVERSE_MESSAGE_SINK_ZK_PATH + "/name"; private static final String REVERSE_MESSAGE_STREAM_TOKEN_ZK_PATH = REVERSE_MESSAGE_STREAM_ZK_PATH + "/token"; private Map secretSourceMap = new HashMap(); private Map nameSourceMap = new HashMap(); private Map secretSinkMap = new HashMap(); private Map nameSinkMap = new HashMap(); private Map streamMap = new HashMap(); private CuratorFramework openedZookeeper; private EventBus componentEventBus; public ConfigManager(CuratorFramework zookeeper) { this.openedZookeeper = zookeeper; //source -> secret PathChildrenCache sourceSecretCache = new PathChildrenCache(zookeeper, REVERSE_MESSAGE_SOURCE_SECRET_ZK_PATH, false); sourceSecretCache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception { onPathChildrenChanged(REVERSE_MESSAGE_SOURCE_SECRET_ZK_PATH); } }); //source -> name PathChildrenCache souceNameCache = new PathChildrenCache(zookeeper, REVERSE_MESSAGE_SOURCE_NAME_ZK_PATH, false); souceNameCache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception { onPathChildrenChanged(REVERSE_MESSAGE_SOURCE_NAME_ZK_PATH); } }); //sink -> secret PathChildrenCache sinkSecretCache = new PathChildrenCache(zookeeper, REVERSE_MESSAGE_SINK_SECRET_ZK_PATH, false); sinkSecretCache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception { onPathChildrenChanged(REVERSE_MESSAGE_SINK_SECRET_ZK_PATH); } }); //sink -> name PathChildrenCache sinkNameCache = new PathChildrenCache(zookeeper, REVERSE_MESSAGE_SINK_NAME_ZK_PATH, false); sinkNameCache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception { onPathChildrenChanged(REVERSE_MESSAGE_SINK_NAME_ZK_PATH); } }); //stream -> token PathChildrenCache streamTokenCache = new PathChildrenCache(zookeeper, REVERSE_MESSAGE_STREAM_TOKEN_ZK_PATH, false); streamTokenCache.getListenable().addListener(new PathChildrenCacheListener() { public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception { onPathChildrenChanged(REVERSE_MESSAGE_STREAM_TOKEN_ZK_PATH); } }); } public EventBus getComponentEventBus() { return componentEventBus; } public void setComponentEventBus(EventBus componentEventBus) { this.componentEventBus = componentEventBus; } public Source getSourceBySecret(String secret) { if (Strings.isNullOrEmpty(secret)) { throw new NullPointerException("the secret can not be null or empty"); } if (this.secretSourceMap.containsKey(secret)) { //local cache return this.secretSourceMap.get(secret); } else { //remote data then save to local cache String sourceStr = getDataFromZK(REVERSE_MESSAGE_SOURCE_SECRET_ZK_PATH + "/" + secret); Source source = GSON.fromJson(sourceStr, Source.class); this.secretSourceMap.put(secret, source); return source; } } public Source getSourceByName(String name) { if (Strings.isNullOrEmpty(name)) { throw new NullPointerException("the name can not be null or empty"); } if (this.nameSourceMap.containsKey(name)) { //local cache return this.nameSourceMap.get(name); } else { //remote data then save to local cache String sourceJson = getDataFromZK(REVERSE_MESSAGE_SOURCE_NAME_ZK_PATH + "/" + name); Source source = GSON.fromJson(sourceJson, Source.class); this.nameSourceMap.put(name, source); return source; } } public Sink getSinkBySecret(String secret) { if (Strings.isNullOrEmpty(secret)) { throw new NullPointerException("the secret can not be null or empty"); } if (this.secretSinkMap.containsKey(secret)) { //local cache return this.secretSinkMap.get(secret); } else { //remote data then save to local cache String sinkJson = getDataFromZK(REVERSE_MESSAGE_SINK_SECRET_ZK_PATH + "/" + secret); Sink sink = GSON.fromJson(sinkJson, Sink.class); this.secretSinkMap.put(secret, sink); return sink; } } public Sink getSinkByName(String name) { if (Strings.isNullOrEmpty(name)) { throw new NullPointerException("the name can not be null or empty"); } if (this.nameSinkMap.containsKey(name)) { //local cache return this.nameSinkMap.get(name); } else { //remote data then save to local cache String sinkJson = getDataFromZK(REVERSE_MESSAGE_SINK_NAME_ZK_PATH + "/" + name); Sink sink = GSON.fromJson(sinkJson, Sink.class); this.nameSinkMap.put(name, sink); return sink; } } public Stream getStreamByToken(String token) { if (Strings.isNullOrEmpty(token)) { throw new NullPointerException("the token can not be null or empty"); } if (this.streamMap.containsKey(token)) { //local cache return this.streamMap.get(token); } else { //remote data then save to local cache String streamJson = getDataFromZK(REVERSE_MESSAGE_STREAM_ZK_PATH + "/" + token); Stream stream = GSON.fromJson(streamJson, Stream.class); this.streamMap.put(token, stream); return stream; } } private void refreshSourceCache(boolean bySecret) { if (bySecret) { for (String secret : this.secretSourceMap.keySet()) { String sourceStr = getDataFromZK(REVERSE_MESSAGE_SOURCE_SECRET_ZK_PATH + "/" + secret); Source source = GSON.fromJson(sourceStr, Source.class); this.secretSourceMap.put(secret, source); } } else { for (String name : this.nameSourceMap.keySet()) { String sourceJson = getDataFromZK(REVERSE_MESSAGE_SOURCE_NAME_ZK_PATH + "/" + name); Source source = GSON.fromJson(sourceJson, Source.class); this.nameSourceMap.put(name, source); } } } private void refreshSinkCache(boolean bySecret) { if (bySecret) { for (String secret : this.secretSinkMap.keySet()) { String sinkJson = getDataFromZK(REVERSE_MESSAGE_SINK_SECRET_ZK_PATH + "/" + secret); Sink sink = GSON.fromJson(sinkJson, Sink.class); this.secretSinkMap.put(secret, sink); } } else { for (String name : this.nameSinkMap.keySet()) { String sinkJson = getDataFromZK(REVERSE_MESSAGE_SINK_NAME_ZK_PATH + "/" + name); Sink sink = GSON.fromJson(sinkJson, Sink.class); this.nameSinkMap.put(name, sink); } } } private void refreshStreamCache() { for (String token : this.streamMap.keySet()) { String streamJson = getDataFromZK(REVERSE_MESSAGE_STREAM_ZK_PATH + "/" + token); Stream stream = GSON.fromJson(streamJson, Stream.class); this.streamMap.put(token, stream); } } public static class Source { private String secret; private String name; private String type; private String appId; private String broadcastable; private String routingKey; public Source() { } public String getSecret() { return secret; } public void setSecret(String secret) { this.secret = secret; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getBroadcastable() { return broadcastable; } public void setBroadcastable(String broadcastable) { this.broadcastable = broadcastable; } public String getRoutingKey() { return routingKey; } public void setRoutingKey(String routingKey) { this.routingKey = routingKey; } } public static class Sink { private String secret; private String name; private String queueName; private String routingKey; private String type; private String appId; private String autoAck; private String msgBodySize; public Sink() { } public String getSecret() { return secret; } public void setSecret(String secret) { this.secret = secret; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getQueueName() { return queueName; } public void setQueueName(String queueName) { this.queueName = queueName; } public String getRoutingKey() { return routingKey; } public void setRoutingKey(String routingKey) { this.routingKey = routingKey; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public boolean isAutoAck() { return autoAck == "1"; } public void setAutoAck(String autoAck) { this.autoAck = autoAck; } public String getMsgBodySize() { return msgBodySize; } public void setMsgBodySize(String msgBodySize) { this.msgBodySize = msgBodySize; } } public static class Stream { private String sourceSecret; private String sourceName; private String sinkSecret; private String sinkName; private String token; public Stream() { } public String getSourceSecret() { return sourceSecret; } public void setSourceSecret(String sourceSecret) { this.sourceSecret = sourceSecret; } public String getSourceName() { return sourceName; } public void setSourceName(String sourceName) { this.sourceName = sourceName; } public String getSinkSecret() { return sinkSecret; } public void setSinkSecret(String sinkSecret) { this.sinkSecret = sinkSecret; } public String getSinkName() { return sinkName; } public void setSinkName(String sinkName) { this.sinkName = sinkName; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } } private String getDataFromZK(String path) { try { logger.debug("path : " + path); return new String(openedZookeeper.getData().forPath(path)); } catch (Exception e) { logger.error(e); } return ""; } private void onPathChildrenChanged(String path) { logger.debug("received path change from zookeeper, key : " + path); String partPath = path.replace(REVERSE_MESSAGE_ZK_PATH + "/", ""); if (partPath.startsWith(SOURCE_EVENT_TYPE)) { boolean refreshBySecret = partPath.endsWith("secret"); refreshSourceCache(refreshBySecret); } else if (partPath.startsWith(SINK_EVENT_TYPE)) { boolean refreshBySecret = partPath.endsWith("secret"); refreshSinkCache(refreshBySecret); } else if (partPath.startsWith(STREAM_EVENT_TYPE)) { refreshStreamCache(); } else { logger.warn("received unknown event type : " + partPath); } } } ================================================ FILE: client/src/main/java/com/messagebus/client/GenericContext.java ================================================ package com.messagebus.client; import com.google.common.eventbus.EventBus; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; /** * generic context. contains both general object * and those object that it owns resource that must be destroyed */ public class GenericContext { private ConfigManager configManager; private Channel channel; private Connection connection; private IMessageReceiveListener noticeListener; private EventBus carryEventBus; private EventBus componentEventBus; public GenericContext() { } public ConfigManager getConfigManager() { return configManager; } public void setConfigManager(ConfigManager configManager) { this.configManager = configManager; } public Channel getChannel() { return channel; } public void setChannel(Channel channel) { this.channel = channel; } public Connection getConnection() { return connection; } public void setConnection(Connection connection) { this.connection = connection; } public EventBus getCarryEventBus() { return carryEventBus; } public void setCarryEventBus(EventBus carryEventBus) { this.carryEventBus = carryEventBus; } public EventBus getComponentEventBus() { return componentEventBus; } public void setComponentEventBus(EventBus componentEventBus) { this.componentEventBus = componentEventBus; } @Override public String toString() { return "GeneralContext{}"; } } ================================================ FILE: client/src/main/java/com/messagebus/client/IMessageReceiveListener.java ================================================ package com.messagebus.client; import com.messagebus.client.message.model.Message; /** * message receive listener interface */ public interface IMessageReceiveListener { public void onMessage(Message message); } ================================================ FILE: client/src/main/java/com/messagebus/client/IRequestListener.java ================================================ package com.messagebus.client; import com.messagebus.client.message.model.Message; /** * Created by yanghua on 3/16/15. */ public interface IRequestListener { public Message onRequest(Message requestMsg); } ================================================ FILE: client/src/main/java/com/messagebus/client/IRpcMessageProcessor.java ================================================ package com.messagebus.client; /** * Created by yanghua on 4/17/15. */ public interface IRpcMessageProcessor { public byte[] onRpcMessage(byte[] in); } ================================================ FILE: client/src/main/java/com/messagebus/client/InnerClient.java ================================================ package com.messagebus.client; import com.google.common.eventbus.EventBus; import com.messagebus.client.event.component.ClientDestroyEvent; import com.messagebus.client.event.component.ClientInitedEvent; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; /** * Created by yanghua on 3/1/15. */ abstract class InnerClient { private static final Log logger = LogFactory.getLog(Messagebus.class); private ConfigManager configManager; private Connection connection; protected EventBus componentEventBus; protected EventBus carryEventBus; private Channel channel; protected GenericContext context; private AtomicBoolean isOpen = new AtomicBoolean(false); public InnerClient() { context = new GenericContext(); } private void open() { if (this.isOpen()) return; try { this.channel = this.connection.createChannel(); context.setChannel(this.channel); } catch (IOException e) { logger.error("create channel error, connection host : " + this.connection.getAddress().getHostAddress() + " connection port : " + this.connection.getPort(), e); throw new RuntimeException(e); } carryEventBus = new EventBus("carryEventBus"); context.setCarryEventBus(carryEventBus); context.setConfigManager(this.configManager); context.setConnection(this.connection); this.isOpen.compareAndSet(false, true); this.componentEventBus.post(new ClientInitedEvent()); } private void close() { //release all resource synchronized (this.channel) { try { if (this.channel != null && this.channel.isOpen()) this.channel.close(); this.carryEventBus = null; this.isOpen.compareAndSet(true, false); this.componentEventBus.post(new ClientDestroyEvent()); } catch (IOException e) { logger.error("close inner client exception : ", e); throw new RuntimeException("close inner client exception : ", e); } catch (TimeoutException e) { logger.error("close inner client exception : ", e); throw new RuntimeException("close inner client exception : ", e); } catch (Exception e) { logger.error("close inner client exception : ", e); throw new RuntimeException("close inner client exception : ", e); } } } public boolean isOpen() { return this.isOpen.get(); } } ================================================ FILE: client/src/main/java/com/messagebus/client/InnerPool.java ================================================ package com.messagebus.client; import com.google.common.eventbus.EventBus; import com.rabbitmq.client.Connection; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; /** * Created by yanghua on 3/5/15. */ class InnerPool extends AbstractPool { public InnerPool(GenericObjectPoolConfig poolConfig, ConfigManager configManager, Connection connection, EventBus componentEventBus) { super(poolConfig, new MessagebusFactory( configManager, connection, componentEventBus) ); } /** * inner class : default messagebus pool */ private static class DefaultMessagebusPool extends GenericObjectPoolConfig { public DefaultMessagebusPool() { setTestWhileIdle(false); setMinEvictableIdleTimeMillis(60000); setTimeBetweenEvictionRunsMillis(30000); setNumTestsPerEvictionRun(-1); } } } ================================================ FILE: client/src/main/java/com/messagebus/client/MessageContext.java ================================================ package com.messagebus.client; import com.google.common.eventbus.EventBus; import com.messagebus.client.message.model.Message; import com.messagebus.client.model.MessageCarryType; import com.rabbitmq.client.Channel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /** * the message context, mostly used in handler chain */ public class MessageContext { private static final Log logger = LogFactory.getLog(MessageContext.class); private String host; private String secret; private String token; private boolean enableTransaction; private Message[] messages; private String consumerTag; private MessageCarryType carryType; private ConfigManager.Source source; private ConfigManager.Sink sink; private ConfigManager.Stream stream; private Channel channel; private IMessageReceiveListener receiveListener; private long timeout; private TimeUnit timeoutUnit; private boolean hasTimeout; private int consumeMsgNum; private List consumeMsgs; private String tempQueueName; private ConfigManager configManager; private IRequestListener requestListener; private EventBus carryEventBus; private Map otherParams = new HashMap(); private boolean isSync = false; private Throwable throwable; public MessageContext() { } public String getHost() { return this.host; } public void setHost(String host) { this.host = host; } public String getSecret() { return secret; } public void setSecret(String secret) { this.secret = secret; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } public boolean isEnableTransaction() { return enableTransaction; } public void setEnableTransaction(boolean enableTransaction) { this.enableTransaction = enableTransaction; } public Message[] getMessages() { return messages; } public void setMessages(Message[] messages) { this.messages = messages; } public Channel getChannel() { return channel; } public void setChannel(Channel channel) { this.channel = channel; } public MessageCarryType getCarryType() { return carryType; } public void setCarryType(MessageCarryType carryType) { this.carryType = carryType; } public ConfigManager.Sink getSink() { return sink; } public void setSink(ConfigManager.Sink sink) { this.sink = sink; } public ConfigManager.Source getSource() { return source; } public void setSource(ConfigManager.Source source) { this.source = source; } public ConfigManager.Stream getStream() { return stream; } public void setStream(ConfigManager.Stream stream) { this.stream = stream; } public Map getOtherParams() { return otherParams; } public IMessageReceiveListener getReceiveListener() { return receiveListener; } public void setReceiveListener(IMessageReceiveListener receiveListener) { this.receiveListener = receiveListener; } public long getTimeout() { return timeout; } public void setTimeout(long timeout) { this.timeout = timeout; } public TimeUnit getTimeoutUnit() { return timeoutUnit; } public void setTimeoutUnit(TimeUnit timeoutUnit) { this.timeoutUnit = timeoutUnit; } public boolean isTimeout() { return hasTimeout; } public void setIsTimeout(boolean hasTimeout) { this.hasTimeout = hasTimeout; } public String getTempQueueName() { return tempQueueName; } public void setTempQueueName(String tempQueueName) { this.tempQueueName = tempQueueName; } public int getConsumeMsgNum() { return consumeMsgNum; } public void setConsumeMsgNum(int consumeMsgNum) { this.consumeMsgNum = consumeMsgNum; } public List getConsumeMsgs() { return consumeMsgs; } public void setConsumeMsgs(List consumeMsgs) { this.consumeMsgs = consumeMsgs; } public boolean isSync() { return isSync; } public void setSync(boolean isSync) { this.isSync = isSync; } public String getConsumerTag() { return consumerTag; } public void setConsumerTag(String consumerTag) { this.consumerTag = consumerTag; } public ConfigManager getConfigManager() { return configManager; } public void setConfigManager(ConfigManager configManager) { this.configManager = configManager; } public IRequestListener getRequestListener() { return requestListener; } public void setRequestListener(IRequestListener requestListener) { this.requestListener = requestListener; } public EventBus getCarryEventBus() { return carryEventBus; } public void setCarryEventBus(EventBus carryEventBus) { this.carryEventBus = carryEventBus; } public Throwable getThrowable() { return throwable; } public void setThrowable(Throwable throwable) { this.throwable = throwable; } @Override public String toString() { return "MessageContext"; } } ================================================ FILE: client/src/main/java/com/messagebus/client/MessageResponseTimeoutException.java ================================================ package com.messagebus.client; public class MessageResponseTimeoutException extends RuntimeException { public MessageResponseTimeoutException() { super(); } public MessageResponseTimeoutException(String message) { super(message); } public MessageResponseTimeoutException(String message, Throwable cause) { super(message, cause); } public MessageResponseTimeoutException(Throwable cause) { super(cause); } } ================================================ FILE: client/src/main/java/com/messagebus/client/Messagebus.java ================================================ package com.messagebus.client; import com.messagebus.client.carry.*; import com.messagebus.client.message.model.Message; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.List; import java.util.concurrent.TimeUnit; /** * the main operator of messagebus client */ public class Messagebus extends InnerClient implements IProducer, IConsumer, IRequester, IResponser, IRpcRequester, IRpcResponser, IPublisher, ISubscriber, IBroadcaster { private static final Log logger = LogFactory.getLog(Messagebus.class); private IProducer producer; private IConsumer consumer; private IPublisher publisher; private ISubscriber subscriber; private IRequester requester; private IResponser responser; private IRpcRequester rpcRequester; private IRpcResponser rpcResponser; private IBroadcaster broadcaster; private Messagebus() { super(); producer = CarryFactory.createProducer(context); consumer = CarryFactory.createConsumer(context); publisher = CarryFactory.createPublisher(context); subscriber = CarryFactory.createSubscriber(context); requester = CarryFactory.createRequester(context); responser = CarryFactory.createResponser(context); rpcRequester = CarryFactory.createRpcRequester(context); rpcResponser = CarryFactory.createRpcResponser(context); broadcaster = CarryFactory.createBroadcaster(context); } /** * 生产消息 * * @param secret 通过管控台申请的自身队列标识 * @param to 目标队列名称(必须通过管控台申请与目标队列的通信权限,否则无法通信) * @param msg 待发送的消息对象 * @param token 自身队列跟目标队列通信需要的token,用于校验单向通信是否被授权 */ @Override public void produce(String secret, String to, Message msg, String token) { producer.produce(secret, to, msg, token); } /** * 批量生产消息 * * @param secret 通过管控台申请的自身队列标识 * @param to 目标队列名称(必须通过管控台申请与目标队列的通信权限,否则无法通信) * @param msgs 待发送的消息对象集合 * @param token 自身队列跟目标队列通信需要的token,用于校验单向通信是否被授权 */ @Override public void batchProduce(String secret, String to, Message[] msgs, String token) { producer.batchProduce(secret, to, msgs, token); } /** * 实时消息消费(Push),该消息消费API被设计为阻塞式的,具体的阻塞时间依赖timeout, * 以及TimeUnit这两个参数确定。被阻塞期间,会产生一定时间内的消息消费的事件处理循环。 * 在这段时间内,只要总线的队列里有消息,都会被推送到客户端来。因此该API的实用场景为:实时消费。 * 需要注意的是,如果实时消费的时间较长,请将timeout的值设置为: Integer.MAX_VALUE。 * 并在一个独立的线程上进行异步消费,然后将该线程的控制权外放给主线程或其他控制线程。 * 另外如果实时消费处于web环境下,推荐将消费线程的启停通过继承ServletContextListener * 类实现一个特定的Listener来加以控制。 * * @param secret 通过管控台申请的自身队列标识 * @param timeout 阻塞超时时间值,该值必须与参数unit一起参考才有意义 * @param unit 阻塞超时时间的单位,该值必须与参数timeout一起参考才有意义 * @param onMessage 收到消息后触发的事件,用于实现自己的消息处理实现 */ @Override public void consume(String secret, long timeout, TimeUnit unit, IMessageReceiveListener onMessage) { consumer.consume(secret, timeout, unit, onMessage); } /** * 同步获取式(Pull)消息消费API,该API提供同步获取expectedNum条消息的功能 * 需要注意的是,该参数expectedNum为单次调用能够获取到的最大值 * * @param secret 通过管控台申请的自身队列标识 * @param expectedNum 希望获取到的消息条数 * @return 消费到的消息对象的集合 */ @Override public List consume(String secret, int expectedNum) { return consumer.consume(secret, expectedNum); } /** * 请求消息API,该接口用于实现常用C/S或B/S架构的request/response的request方, * 当消息发送后,它阻塞等待响应消息,直到: * - 收到响应 * - 等待指定的时间后超时(抛出MessageResponseTimeoutException异常) * * @param secret 通过管控台申请的自身队列标识 * @param to 目标队列的队列名称 * @param msg 请求消息对象 * @param token 自身队列跟目标队列通信需要的token,用于校验单向通信是否被授权 * @param timeout 指定的超时时间,默认单位为秒 * @return 获取到的响应消息对象 * @throws MessageResponseTimeoutException 当超过指定的timeout时间后, * 仍未收到响应,将抛出该异常 */ @Override public Message request(String secret, String to, Message msg, String token, long timeout) throws MessageResponseTimeoutException { return requester.request(secret, to, msg, token, timeout); } /** * 应答/响应API,该接口用于实现request/response的response方,因为response方通常需要一个事件处理循环, * 所以该API也是阻塞式的,阻塞的时间依据参数timeout以及timeUnit联合确定。 * 如果阻塞的时间非常长,请将timeout设置为Integer.MAX_VALUE, * 然后以一个独立的事件处理线程来控制它,并将该事件处理线程的控制权暴露给主控线程 * * @param secret 通过管控台申请的自身队列标识 * @param onRequest 当前请求消息到来时触发的时间,用于获取请求消息,并已返回值的方式返回响应消息 * @param timeout 阻塞超时时间值,该值必须与参数unit一起参考才有意义 * @param timeUnit 阻塞超时时间的单位,该值必须与参数timeout一起参考才有意义 */ @Override public void response(String secret, IRequestListener onRequest, long timeout, TimeUnit timeUnit) { responser.response(secret, onRequest, timeout, timeUnit); } /** * 发布消息API,该接口会将消息发布给所有通过管控台申请的所有订阅者 * * @param secret 通过管控台申请的自身队列标识 * @param msgs 要发布的消息数组 */ @Override public void publish(String secret, Message[] msgs) { publisher.publish(secret, msgs); } /** * 订阅消息API,这里假设订阅的场景具有较强的实时性,因此它以阻塞式的事件方式提供调用 * 阻塞的时间依据参数timeout以及timeUnit联合确定。 * 如果阻塞的时间非常长,请将timeout设置为Integer.MAX_VALUE, * 然后以一个独立的处理线程来控制它,并将该处理线程的控制权暴露给主控线程 * * @param secret 通过管控台申请的自身队列标识 * @param from * @param token * @param onMessage 订阅某个频道队列(Channel Queue)后,收到消息的事件处理器 * @param timeout 阻塞超时时间值,该值必须与参数unit一起参考才有意义 * @param unit 阻塞超时时间的单位,该值必须与参数timeout一起参考才有意义 */ @Override public void subscribe(String secret, String from, String token, IMessageReceiveListener onMessage, long timeout, TimeUnit unit) { subscriber.subscribe(secret, from, token, onMessage, timeout, unit); } /** * 广播消息的接口,广播权限需要从管控台申请 * * @param secret 通过管控台申请的自身队列标识 * @param msgs 待广播的消息对象数组 */ @Override public void broadcast(String secret, Message[] msgs) { broadcaster.broadcast(secret, msgs); } /** * 广播消息接收API,注意:不是每个队列都能收到广播消息。通常情况下, * 只有能获取消息的队列才能收到广播消息,广播消息不是必须要求消费的,但推荐在调用消息总线的所有API时, * 设置广播消息处理器。如果不设置该处理器,在收到广播消息时,广播消息将被丢弃! * * @param notificationListener 广播消息接收处理器 */ @Deprecated public void setNotificationListener(IMessageReceiveListener notificationListener) { throw new UnsupportedOperationException("this interface has been deprecated! "); } /** * 支持JSON协议的远程过程调用(JSON-RPC)的请求接口,用于请求一个远程方法,并指定一个获取响应的超时时间。 * 注意,RPC的请求方需要通过管控台授权,拿到token才能发起请求,另外参数顺序必须与目标方法的参数顺序一致。 * * @param secret 通过管控台申请的自身队列标识 * @param target 目标队列 * @param methodName 目标方法名称 * @param params 目标方法的处理参数 * @param token 自身队列跟目标队列通信需要的token,用于校验单向通信是否被授权 * @param timeoutOfMilliSecond 超时的毫秒数 * @return 远程方法调用的返回值,通常只支持:String/Object/Map这些简单java原生对象格式 */ @Override public Object call(String secret, String target, String methodName, Object[] params, String token, long timeoutOfMilliSecond) { return rpcRequester.call(secret, target, methodName, params, token, timeoutOfMilliSecond); } /** * 支持JSON协议的远程过程调用(JSON-RPC)的响应接口,用于作为一个远程方法的响应方,因此为阻塞式事件处理形式。 * 阻塞的时间依据参数timeout以及timeUnit联合确定。 * 如果阻塞的时间非常长,请将timeout设置为Integer.MAX_VALUE, * 然后以一个独立的处理线程来控制它,并将该处理线程的控制权暴露给主控线程 * * @param secret 通过管控台申请的自身队列标识 * @param clazzOfInterface 用于响应处理的接口的Class实例 * @param serviceProvider 用于响应处理的服务提供者对象,该对象必须实现clazzOfInterface对应的接口 * @param timeout 阻塞超时时间值,该值必须与参数unit一起参考才有意义 * @param timeUnit 阻塞超时时间的单位,该值必须与参数timeout一起参考才有意义 */ @Override public void callback(String secret, Class clazzOfInterface, Object serviceProvider, long timeout, TimeUnit timeUnit) { rpcResponser.callback(secret, clazzOfInterface, serviceProvider, timeout, timeUnit); } /** * 原始数据格式请求API,该API为request的原始形式,另一个作用是辅助实现以消息总线作为底层通信的 * 第三方RPC框架的接入。具体实现可参考package: * com.messagebus.client.extension.thrift.TAMQPClientTransport * * @param secret 通过管控台申请的自身队列标识 * @param target 目标队列 * @param requestMsg 请求消息被序列化后的字节数组 * @param token 自身队列跟目标队列通信需要的token,用于校验单向通信是否被授权 * @param timeoutOfMilliSecond 等待响应的超时时间(毫秒数) * @return 响应消息被序列化后的字节数组 */ @Override public byte[] primitiveRequest(String secret, String target, byte[] requestMsg, String token, long timeoutOfMilliSecond) { return requester.primitiveRequest(secret, target, requestMsg, token, timeoutOfMilliSecond); } /** * 用于构建RPC响应服务器的帮助方法,该方法用于辅助第三方RPC框架来构造用于消息总线的通信方式以及事件处理循环 * * @param secret 通过管控台申请的自身队列标识 * @param rpcMsgProcessor 一个RPC消息处理器,当有RPC请求消息到来时触发 * @return 返回一个被包装过的RPCServer */ @Override public WrappedRpcServer buildRpcServer(String secret, IRpcMessageProcessor rpcMsgProcessor) { return rpcResponser.buildRpcServer(secret, rpcMsgProcessor); } /** * 注册消息总线支持的事件处理器;事件处理器对象遵循Guava的事件订阅模式(@Subscribe) * * @param eventProcessor 事件处理器对象的实例 */ public void registerEventProcessor(Object eventProcessor) { componentEventBus.register(eventProcessor); } /** * 注销消息总线支持的事件处理器; * * @param eventProcessor 事件处理器对象的实例 */ public void unregisterEventProcessor(Object eventProcessor) { componentEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/MessagebusConnectedFailedException.java ================================================ package com.messagebus.client; /** * the exception about messagebus connect failed exception */ public class MessagebusConnectedFailedException extends Exception { public MessagebusConnectedFailedException() { super(); } public MessagebusConnectedFailedException(String message) { super(message); } public MessagebusConnectedFailedException(String message, Throwable cause) { super(message, cause); } public MessagebusConnectedFailedException(Throwable cause) { super(cause); } } ================================================ FILE: client/src/main/java/com/messagebus/client/MessagebusFactory.java ================================================ package com.messagebus.client; import com.google.common.eventbus.EventBus; import com.messagebus.common.ExceptionHelper; import com.rabbitmq.client.Connection; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.DefaultPooledObject; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * Created by yanghua on 3/5/15. */ class MessagebusFactory implements PooledObjectFactory { private static final Log logger = LogFactory.getLog(MessagebusFactory.class.getName()); private ConfigManager configManager; private Connection connection; private EventBus componentEventBus; private final Method openMethod; private final Method closeMethod; public MessagebusFactory(ConfigManager configManager, Connection connection, EventBus componentEventBus) { this.configManager = configManager; this.connection = connection; this.componentEventBus = componentEventBus; try { openMethod = Messagebus.class.getSuperclass().getDeclaredMethod("open"); openMethod.setAccessible(true); closeMethod = Messagebus.class.getSuperclass().getDeclaredMethod("close"); closeMethod.setAccessible(true); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } @Override public PooledObject makeObject() throws Exception { Constructor privateCtor = Messagebus.class.getDeclaredConstructor(); privateCtor.setAccessible(true); Messagebus client = privateCtor.newInstance(); privateCtor.setAccessible(false); Class superClient = Messagebus.class.getSuperclass(); //set private field Field configManagerField = superClient.getDeclaredField("configManager"); configManagerField.setAccessible(true); configManagerField.set(client, this.configManager); configManagerField.setAccessible(false); Field connectionField = superClient.getDeclaredField("connection"); connectionField.setAccessible(true); connectionField.set(client, this.connection); connectionField.setAccessible(false); Field componentEventBus = superClient.getDeclaredField("componentEventBus"); componentEventBus.setAccessible(true); componentEventBus.set(client, this.componentEventBus); componentEventBus.setAccessible(false); openMethod.invoke(client); return new DefaultPooledObject(client); } @Override public void destroyObject(PooledObject pooledObject) throws Exception { Messagebus client = pooledObject.getObject(); if (client != null) { if (client.isOpen()) { closeMethod.invoke(client); } } } @Override public boolean validateObject(PooledObject pooledObject) { Messagebus client = pooledObject.getObject(); if (client != null && !client.isOpen()) { try { openMethod.invoke(client); } catch (IllegalAccessException e) { ExceptionHelper.logException(logger, e, "validateObject"); } catch (InvocationTargetException e) { ExceptionHelper.logException(logger, e, "validateObject"); } } return client != null && client.isOpen(); } @Override public void activateObject(PooledObject pooledObject) throws Exception { } @Override public void passivateObject(PooledObject pooledObject) throws Exception { } } ================================================ FILE: client/src/main/java/com/messagebus/client/MessagebusPool.java ================================================ package com.messagebus.client; import com.google.common.base.Strings; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.EventBus; import com.messagebus.client.event.component.ClientDestroyEventProcessor; import com.messagebus.client.event.component.ClientInitedEventProcessor; import com.messagebus.client.event.component.NoticeEvent; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.RandomHelper; import com.messagebus.interactor.proxy.ProxyConsumer; import com.rabbitmq.client.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.imps.CuratorFrameworkState; import org.apache.curator.retry.ExponentialBackoffRetry; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.TimeoutException; /** * Created by yanghua on 3/18/15. */ public class MessagebusPool { private static final Log logger = LogFactory.getLog(MessagebusPool.class); private static final String INNER_EXCHANGE_NAME = "exchange.proxy.message.inner"; private static final String EVENT_ROUTING_KEY_NAME = "routingkey.proxy.message.inner.event"; private static final String NOTICE_ROUTING_KEY_NAME = "routingkey.proxy.message.inner.notice"; private static final String EVENT_QUEUE_NAME_PREFIX = "autodelete_exclusive.queue.proxy.message.inner.event."; private static final String NOTICE_QUEUE_NAME_PREFIX = "autodelete_exclusive.queue.proxy.message.inner.notice."; private static final String EVENT_CONSUMER_TAG_PREFIX = "tag.proxy.message.inner.event."; private static final String NOTICE_CONSUMER_TAG_PREFIX = "tag.proxy.message.inner.notice."; private static final String MESSAGE_SERVER_ZK_PATH = "/component/message/server"; private com.rabbitmq.client.Address[] innerAddresses; private ConfigManager configManager; private CuratorFramework zookeeper; private Connection connection; private Channel innerChannel; private EventBus componentEventBus; private RemoteNoticeListener remoteNoticeListener; protected InnerPool innerPool; public MessagebusPool(String zkConnectionStr, GenericObjectPoolConfig poolConfig) { this.init(zkConnectionStr); this.innerPool = new InnerPool(poolConfig, configManager, connection, componentEventBus); } public void registerComponentEventListener(Object listener) { this.componentEventBus.register(listener); } public Messagebus getResource() { return this.innerPool.getResource(); } public void returnResource(Messagebus client) { this.innerPool.returnResource(client); } public void destroy() { this.remoteNoticeListener.shutdown(); this.innerPool.destroy(); try { if (this.innerChannel != null && this.innerChannel.isOpen()) { this.innerChannel.close(); } if (this.connection != null && this.connection.isOpen()) { this.connection.close(); } if (zookeeper != null && zookeeper.getState().equals(CuratorFrameworkState.STARTED)) { this.zookeeper.close(); } } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } catch (TimeoutException e) { logger.error(e); throw new RuntimeException(e); } catch (Exception e) { logger.error(e); throw new RuntimeException(e); } } protected void init(String zkConnectionStr) { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); zookeeper = CuratorFrameworkFactory.newClient( zkConnectionStr, retryPolicy); try { zookeeper.start(); String mqConnectionStr = new String( zookeeper.getData().forPath(MESSAGE_SERVER_ZK_PATH) ); if (Strings.isNullOrEmpty(mqConnectionStr)) { logger.info("can not get mq connection info"); throw new RuntimeException("can not get mq connection info"); } String[] hostPortPairArr = mqConnectionStr.split(","); this.innerAddresses = new com.rabbitmq.client.Address[hostPortPairArr.length]; for (int i = 0; i < hostPortPairArr.length; i++) { String[] hostPortArr = hostPortPairArr[i].split(":"); com.rabbitmq.client.Address address = new com.rabbitmq.client.Address( hostPortArr[0], Integer.parseInt(hostPortArr[1]) ); this.innerAddresses[i] = address; } this.componentEventBus = new AsyncEventBus("componentEventBus", Executors.newSingleThreadExecutor()); this.configManager = new ConfigManager(zookeeper); this.configManager.setComponentEventBus(this.componentEventBus); this.registerComponentEventProcessor(); ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setAutomaticRecoveryEnabled(true); connectionFactory.setTopologyRecoveryEnabled(true); connectionFactory.setConnectionTimeout(60000); connectionFactory.setRequestedHeartbeat(10); this.connection = connectionFactory.newConnection(innerAddresses); this.innerChannel = this.connection.createChannel(); } catch (IOException e) { logger.error("init message pool exception ", e); throw new RuntimeException("init message pool exception", e); } catch (TimeoutException e) { logger.error("init message pool exception ", e); throw new RuntimeException("init message pool exception ", e); } catch (Exception e) { logger.error("init message pool exception ", e); throw new RuntimeException("init message pool exception ", e); } this.remoteNoticeListener = new RemoteNoticeListener(); //start listen remote notice this.remoteNoticeListener.start(); } private void registerComponentEventProcessor() { this.componentEventBus.register(new ClientDestroyEventProcessor()); this.componentEventBus.register(new ClientInitedEventProcessor()); } private class RemoteNoticeListener implements Runnable { private Thread currentThread; public RemoteNoticeListener() { this.currentThread = new Thread(this); this.currentThread.setDaemon(true); this.currentThread.setName("remote notice listener"); } @Override public void run() { try { String queueName = NOTICE_QUEUE_NAME_PREFIX + RandomHelper.randomNumberAndCharacter(5); //declare a non-durable auto-delete & exclusive queue then consume innerChannel.queueDeclare(queueName, false, true, true, null); Map matchHeader = new HashMap(2); matchHeader.put("x-match", "all"); matchHeader.put("type", "notice"); innerChannel.queueBind(queueName, INNER_EXCHANGE_NAME, NOTICE_ROUTING_KEY_NAME, matchHeader); String randomTag = RandomHelper.randomNumberAndCharacter(6); QueueingConsumer consumer = ProxyConsumer.consume( innerChannel, queueName, true, NOTICE_CONSUMER_TAG_PREFIX + randomTag); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); final Message msg = MessageFactory.createMessage(delivery); if (msg == null) continue; NoticeEvent noticeEvent = new NoticeEvent(); noticeEvent.setMsg(msg); componentEventBus.post(noticeEvent); } } catch (InterruptedException e) { } catch (ShutdownSignalException e) { logger.error(e); } catch (Exception e) { logger.error(e); } } public void start() { this.currentThread.start(); } public void shutdown() { this.currentThread.interrupt(); } } } ================================================ FILE: client/src/main/java/com/messagebus/client/MessagebusSinglePool.java ================================================ package com.messagebus.client; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; /** * Created by yanghua on 3/18/15. */ public class MessagebusSinglePool extends MessagebusPool { public MessagebusSinglePool(String zkConnectionStr) { super(zkConnectionStr, new DefaultMessagebusPoolConfig()); } /** * inner class : single messagebus instance config */ private static class DefaultMessagebusPoolConfig extends GenericObjectPoolConfig { public DefaultMessagebusPoolConfig() { setTestWhileIdle(false); setMaxTotal(1); setMinEvictableIdleTimeMillis(60000); setTimeBetweenEvictionRunsMillis(30000); setNumTestsPerEvictionRun(-1); } } } ================================================ FILE: client/src/main/java/com/messagebus/client/MessagebusUnOpenException.java ================================================ package com.messagebus.client; /** * the exception about messagebus unopened, mostly for semantic */ public class MessagebusUnOpenException extends RuntimeException { public MessagebusUnOpenException() { super(); } public MessagebusUnOpenException(String message) { super(message); } public MessagebusUnOpenException(String message, Throwable cause) { super(message, cause); } public MessagebusUnOpenException(Throwable cause) { super(cause); } } ================================================ FILE: client/src/main/java/com/messagebus/client/WrappedRpcServer.java ================================================ package com.messagebus.client; import com.messagebus.common.ExceptionHelper; import com.rabbitmq.client.RpcServer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; /** * Created by yanghua on 4/16/15. */ public class WrappedRpcServer { private static final Log logger = LogFactory.getLog(WrappedRpcServer.class); private RpcServer innerRpcServer; private WrappedRpcServer(RpcServer rpcServer) { this.innerRpcServer = rpcServer; } public void mainLoop() { try { innerRpcServer.mainloop(); } catch (IOException e) { ExceptionHelper.logException(logger, e, "mainLoop"); throw new RuntimeException(e); } } public void terminateMainloop() { innerRpcServer.terminateMainloop(); } public void close() { try { innerRpcServer.close(); } catch (IOException e) { ExceptionHelper.logException(logger, e, "close"); throw new RuntimeException(e); } } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/AbstractMessageCarryer.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.GenericContext; import com.messagebus.client.MessageContext; /** * abstract class of message carryer * in message bus, we treat producer and consumer as carryer */ abstract class AbstractMessageCarryer { private GenericContext context; protected MessageContext initMessageContext() { MessageContext msgCtx = new MessageContext(); msgCtx.setConfigManager(this.context.getConfigManager()); msgCtx.setChannel(this.context.getChannel()); msgCtx.setHost(this.context.getConnection().getAddress().getHostAddress()); msgCtx.setCarryEventBus(this.context.getCarryEventBus()); return msgCtx; } public GenericContext getContext() { return context; } public void setContext(GenericContext context) { this.context = context; } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/CarryFactory.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.GenericContext; /** * Created by yanghua on 4/2/15. */ public class CarryFactory { public static IBroadcaster createBroadcaster(GenericContext context) { GenericBroadcaster broadcaster = new GenericBroadcaster(); broadcaster.setContext(context); return broadcaster; } public static IConsumer createConsumer(GenericContext context) { GenericConsumer consumer = new GenericConsumer(); consumer.setContext(context); return consumer; } public static IProducer createProducer(GenericContext context) { GenericProducer producer = new GenericProducer(); producer.setContext(context); return producer; } public static IPublisher createPublisher(GenericContext context) { GenericPublisher publisher = new GenericPublisher(); publisher.setContext(context); return publisher; } public static IRequester createRequester(GenericContext context) { GenericRequester requester = new GenericRequester(); requester.setContext(context); return requester; } public static IResponser createResponser(GenericContext context) { GenericResponser responser = new GenericResponser(); responser.setContext(context); return responser; } public static IRpcRequester createRpcRequester(GenericContext context) { GenericRpcRequester rpcRequester = new GenericRpcRequester(); rpcRequester.setContext(context); return rpcRequester; } public static IRpcResponser createRpcResponser(GenericContext context) { GenericRpcResponser rpcResponser = new GenericRpcResponser(); rpcResponser.setContext(context); return rpcResponser; } public static ISubscriber createSubscriber(GenericContext context) { GenericSubscriber subscriber = new GenericSubscriber(); subscriber.setContext(context); return subscriber; } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/GenericBroadcaster.java ================================================ package com.messagebus.client.carry; import com.google.common.eventbus.EventBus; import com.messagebus.client.MessageContext; import com.messagebus.client.event.carry.BroadcastEventProcessor; import com.messagebus.client.event.carry.CommonEventProcessor; import com.messagebus.client.message.model.Message; import com.messagebus.client.model.MessageCarryType; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; class GenericBroadcaster extends AbstractMessageCarryer implements IBroadcaster { private static final Log logger = LogFactory.getLog(GenericBroadcaster.class); public GenericBroadcaster() { } @Override public void broadcast(String secret, Message[] msgs) { MessageContext ctx = initMessageContext(); ctx.setSecret(secret); ctx.setCarryType(MessageCarryType.BROADCAST); ctx.setSource(this.getContext().getConfigManager().getSourceBySecret(secret)); ctx.setMessages(msgs); this.innerBroadcast(ctx); } private void innerBroadcast(MessageContext ctx) { EventBus carryEventBus = this.getContext().getCarryEventBus(); BroadcastEventProcessor eventProcessor = new BroadcastEventProcessor(); carryEventBus.register(eventProcessor); CommonEventProcessor.MsgIdGenerateEvent msgIdGenerateEvent = new CommonEventProcessor.MsgIdGenerateEvent(); BroadcastEventProcessor.ValidateEvent validateEvent = new BroadcastEventProcessor.ValidateEvent(); BroadcastEventProcessor.PermissionCheckEvent permissionCheckEvent = new BroadcastEventProcessor.PermissionCheckEvent(); BroadcastEventProcessor.BroadcastEvent broadcastEvent = new BroadcastEventProcessor.BroadcastEvent(); msgIdGenerateEvent.setMessageContext(ctx); validateEvent.setMessageContext(ctx); permissionCheckEvent.setMessageContext(ctx); broadcastEvent.setMessageContext(ctx); carryEventBus.post(msgIdGenerateEvent); carryEventBus.post(validateEvent); carryEventBus.post(permissionCheckEvent); carryEventBus.post(broadcastEvent); carryEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/GenericConsumer.java ================================================ package com.messagebus.client.carry; import com.google.common.eventbus.EventBus; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.MessageContext; import com.messagebus.client.event.carry.CommonEventProcessor; import com.messagebus.client.event.carry.ConsumeEventProcessor; import com.messagebus.client.message.model.Message; import com.messagebus.client.model.MessageCarryType; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.List; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 6/26/15. */ class GenericConsumer extends AbstractMessageCarryer implements IConsumer { private static final Log logger = LogFactory.getLog(GenericConsumer.class); @Override public void consume(String secret, long timeout, TimeUnit unit, IMessageReceiveListener onMessage) { logger.debug("current secret is : " + secret); final MessageContext ctx = initMessageContext(); ctx.setSecret(secret); ctx.setCarryType(MessageCarryType.CONSUME); ctx.setSink(this.getContext().getConfigManager().getSinkBySecret(secret)); ctx.setReceiveListener(onMessage); ctx.setTimeout(timeout); ctx.setTimeoutUnit(unit); ctx.setSync(false); this.innerConsume(ctx); } @Override public List consume(String secret, int expectedNum) { final MessageContext ctx = initMessageContext(); ctx.setSecret(secret); ctx.setCarryType(MessageCarryType.CONSUME); ctx.setSink(this.getContext().getConfigManager().getSinkBySecret(secret)); ctx.setConsumeMsgNum(expectedNum); ctx.setSync(true); this.innerConsume(ctx); return ctx.getConsumeMsgs(); } private void innerConsume(MessageContext ctx) { EventBus carryEventBus = this.getContext().getCarryEventBus(); //register event processor ConsumeEventProcessor eventProcessor = new ConsumeEventProcessor(); carryEventBus.register(eventProcessor); //init events ConsumeEventProcessor.ValidateEvent validateEvent = new ConsumeEventProcessor.ValidateEvent(); ConsumeEventProcessor.PermissionCheckEvent permissionCheckEvent = new ConsumeEventProcessor.PermissionCheckEvent(); ConsumeEventProcessor.TagGenerateEvent tagGenerateEvent = new ConsumeEventProcessor.TagGenerateEvent(); CommonEventProcessor.PreConsumeEvent perConsumeEvent = new CommonEventProcessor.PreConsumeEvent(); validateEvent.setMessageContext(ctx); permissionCheckEvent.setMessageContext(ctx); tagGenerateEvent.setMessageContext(ctx); perConsumeEvent.setMessageContext(ctx); //arrange event order and emit carryEventBus.post(validateEvent); carryEventBus.post(permissionCheckEvent); carryEventBus.post(tagGenerateEvent); carryEventBus.post(perConsumeEvent); if (ctx.isSync()) { ConsumeEventProcessor.SyncConsumeEvent syncConsumeEvent = new ConsumeEventProcessor.SyncConsumeEvent(); syncConsumeEvent.setMessageContext(ctx); carryEventBus.post(syncConsumeEvent); } else { ConsumeEventProcessor.AsyncMessageLoopEvent asyncMessageLoopEvent = new ConsumeEventProcessor.AsyncMessageLoopEvent(); asyncMessageLoopEvent.setMessageContext(ctx); carryEventBus.post(asyncMessageLoopEvent); } carryEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/GenericProducer.java ================================================ package com.messagebus.client.carry; import com.google.common.eventbus.EventBus; import com.messagebus.client.ConfigManager; import com.messagebus.client.MessageContext; import com.messagebus.client.event.carry.ProduceEventProcessor; import com.messagebus.client.message.model.Message; import com.messagebus.client.model.MessageCarryType; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * a generic producer implements the IProducer interface */ class GenericProducer extends AbstractMessageCarryer implements IProducer { private static final Log logger = LogFactory.getLog(GenericProducer.class); /** * simple producer just produces a message * * @param secret * @param to the message's destination * @param msg a general message * @param token */ @Override public void produce(String secret, String to, Message msg, String token) { MessageContext ctx = this.initMsgContext(secret, to, token); ctx.setMessages(new Message[]{msg}); innerProduce(ctx); } /** * a producer produces a set of messages * * @param secret * @param to the message's destination * @param msgs a general message's array * @param token */ @Override public void batchProduce(String secret, String to, Message[] msgs, String token) { MessageContext context = this.initMsgContext(secret, to, token); context.setMessages(msgs); innerProduce(context); } private MessageContext initMsgContext(String secret, String to, String token) { MessageContext context = initMessageContext(); context.setSecret(secret); context.setCarryType(MessageCarryType.PRODUCE); context.setSource(this.getContext().getConfigManager().getSourceBySecret(secret)); ConfigManager.Sink sink = this.getContext().getConfigManager().getSinkByName(to); context.setSink(sink); context.setToken(token); context.setStream(this.getContext().getConfigManager().getStreamByToken(token)); return context; } private void innerProduce(MessageContext ctx) { EventBus carryEventBus = this.getContext().getCarryEventBus(); //register event processor ProduceEventProcessor eventProcessor = new ProduceEventProcessor(); carryEventBus.register(eventProcessor); //init events ProduceEventProcessor.ValidateEvent validateEvent = new ProduceEventProcessor.ValidateEvent(); ProduceEventProcessor.MsgBodySizeCheckEvent msgBodySizeCheckEvent = new ProduceEventProcessor.MsgBodySizeCheckEvent(); ProduceEventProcessor.PermissionCheckEvent permissionCheckEvent = new ProduceEventProcessor.PermissionCheckEvent(); ProduceEventProcessor.MsgIdGenerateEvent msgIdGenerateEvent = new ProduceEventProcessor.MsgIdGenerateEvent(); ProduceEventProcessor.MsgBodyCompressEvent msgBodyCompressEvent = new ProduceEventProcessor.MsgBodyCompressEvent(); ProduceEventProcessor.ProduceEvent produceEvent = new ProduceEventProcessor.ProduceEvent(); validateEvent.setMessageContext(ctx); msgBodySizeCheckEvent.setMessageContext(ctx); permissionCheckEvent.setMessageContext(ctx); msgIdGenerateEvent.setMessageContext(ctx); msgBodyCompressEvent.setMessageContext(ctx); produceEvent.setMessageContext(ctx); //arrange event order and emit! carryEventBus.post(validateEvent); carryEventBus.post(msgBodySizeCheckEvent); carryEventBus.post(permissionCheckEvent); carryEventBus.post(msgIdGenerateEvent); carryEventBus.post(msgBodyCompressEvent); carryEventBus.post(produceEvent); //unregister event processor carryEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/GenericPublisher.java ================================================ package com.messagebus.client.carry; import com.google.common.eventbus.EventBus; import com.messagebus.client.MessageContext; import com.messagebus.client.event.carry.CommonEventProcessor; import com.messagebus.client.event.carry.PublishEventProcessor; import com.messagebus.client.message.model.Message; import com.messagebus.client.model.MessageCarryType; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; class GenericPublisher extends AbstractMessageCarryer implements IPublisher { private static final Log logger = LogFactory.getLog(GenericPublisher.class); public GenericPublisher() { } @Override public void publish(String secret, Message[] msgs) { MessageContext ctx = initMessageContext(); ctx.setSecret(secret); ctx.setCarryType(MessageCarryType.PUBLISH); ctx.setSource(this.getContext().getConfigManager().getSourceBySecret(secret)); ctx.setMessages(msgs); this.innerPublish(ctx); } private void innerPublish(MessageContext context) { EventBus carryEventBus = this.getContext().getCarryEventBus(); //register event processor PublishEventProcessor eventProcessor = new PublishEventProcessor(); carryEventBus.register(eventProcessor); //init events CommonEventProcessor.MsgIdGenerateEvent msgIdGenerateEvent = new CommonEventProcessor.MsgIdGenerateEvent(); PublishEventProcessor.ValidateEvent validateEvent = new PublishEventProcessor.ValidateEvent(); PublishEventProcessor.PermissionCheckEvent permissionCheckEvent = new PublishEventProcessor.PermissionCheckEvent(); PublishEventProcessor.PublishEvent publishEvent = new PublishEventProcessor.PublishEvent(); msgIdGenerateEvent.setMessageContext(context); validateEvent.setMessageContext(context); permissionCheckEvent.setMessageContext(context); publishEvent.setMessageContext(context); //arrange event order and emit carryEventBus.post(msgIdGenerateEvent); carryEventBus.post(validateEvent); carryEventBus.post(permissionCheckEvent); carryEventBus.post(publishEvent); //unregister carryEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/GenericRequester.java ================================================ package com.messagebus.client.carry; import com.google.common.eventbus.EventBus; import com.messagebus.client.ConfigManager; import com.messagebus.client.MessageContext; import com.messagebus.client.MessageResponseTimeoutException; import com.messagebus.client.event.carry.CommonEventProcessor; import com.messagebus.client.event.carry.RequestEventProcessor; import com.messagebus.client.message.model.Message; import com.messagebus.client.model.MessageCarryType; import com.messagebus.common.Constants; import com.messagebus.common.ExceptionHelper; import com.rabbitmq.client.RpcClient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; class GenericRequester extends AbstractMessageCarryer implements IRequester { private static final Log logger = LogFactory.getLog(GenericRequester.class); public GenericRequester() { } /** * send a request and got a response * * @param secret * @param to send to destination * @param msg request message * @param token * @param timeout response wait timeout @return Message the response * @throws com.messagebus.client.MessageResponseTimeoutException */ @Override public Message request(String secret, String to, Message msg, String token, long timeout) throws MessageResponseTimeoutException { MessageContext ctx = initMessageContext(); ctx.setSecret(secret); ctx.setToken(token); ctx.setCarryType(MessageCarryType.REQUEST); ctx.setSource(this.getContext().getConfigManager().getSourceBySecret(secret)); ctx.setStream(this.getContext().getConfigManager().getStreamByToken(token)); ConfigManager.Sink sink = this.getContext().getConfigManager().getSinkByName(to); ctx.setSink(sink); ctx.setTimeout(timeout); ctx.setMessages(new Message[]{msg}); this.innerRequest(ctx); if (ctx.isTimeout() || ctx.getConsumeMsgs() == null || ctx.getConsumeMsgs().size() == 0) throw new MessageResponseTimeoutException("message request time out."); return ctx.getConsumeMsgs().get(0); } @Override public byte[] primitiveRequest(String secret, String target, byte[] requestMsg, String token, long timeoutOfMilliSecond) { ConfigManager.Source source = this.getContext().getConfigManager().getSourceBySecret(secret); ConfigManager.Sink sink = this.getContext().getConfigManager().getSinkByName(target); RpcClient innerRpcClient = null; try { innerRpcClient = new RpcClient(this.getContext().getChannel(), Constants.PROXY_EXCHANGE_NAME, sink.getRoutingKey(), (int) timeoutOfMilliSecond); return innerRpcClient.primitiveCall(requestMsg); } catch (IOException e) { ExceptionHelper.logException(logger, e, "primitive request "); } catch (TimeoutException e) { logger.info("primitiveRequest timeout : " + "[secret] " + secret + " [target] " + target); } finally { try { if (innerRpcClient != null) innerRpcClient.close(); } catch (IOException e) { ExceptionHelper.logException(logger, e, "primitive request finally close inner rpc client"); } } return new byte[0]; } private void innerRequest(MessageContext ctx) { EventBus carryEventBus = this.getContext().getCarryEventBus(); //register event processor RequestEventProcessor eventProcessor = new RequestEventProcessor(); carryEventBus.register(eventProcessor); RequestEventProcessor.ValidateEvent validateEvent = new RequestEventProcessor.ValidateEvent(); CommonEventProcessor.MsgBodySizeCheckEvent msgBodySizeCheckEvent = new CommonEventProcessor.MsgBodySizeCheckEvent(); RequestEventProcessor.PermissionCheckEvent permissionCheckEvent = new RequestEventProcessor.PermissionCheckEvent(); CommonEventProcessor.MsgIdGenerateEvent msgIdGenerateEvent = new CommonEventProcessor.MsgIdGenerateEvent(); RequestEventProcessor.TempQueueInitializeEvent tempQueueInitializeEvent = new RequestEventProcessor.TempQueueInitializeEvent(); CommonEventProcessor.MsgBodyCompressEvent msgBodyCompressEvent = new CommonEventProcessor.MsgBodyCompressEvent(); RequestEventProcessor.RequestEvent requestEvent = new RequestEventProcessor.RequestEvent(); CommonEventProcessor.TagGenerateEvent tagGenerateEvent = new CommonEventProcessor.TagGenerateEvent(); RequestEventProcessor.BlockAndTimeoutResponseEvent blockAndTimeoutResponseEvent = new RequestEventProcessor.BlockAndTimeoutResponseEvent(); validateEvent.setMessageContext(ctx); msgBodySizeCheckEvent.setMessageContext(ctx); permissionCheckEvent.setMessageContext(ctx); msgIdGenerateEvent.setMessageContext(ctx); tempQueueInitializeEvent.setMessageContext(ctx); msgBodyCompressEvent.setMessageContext(ctx); requestEvent.setMessageContext(ctx); tagGenerateEvent.setMessageContext(ctx); blockAndTimeoutResponseEvent.setMessageContext(ctx); //arrange event order and emit carryEventBus.post(validateEvent); carryEventBus.post(msgBodySizeCheckEvent); carryEventBus.post(permissionCheckEvent); carryEventBus.post(msgIdGenerateEvent); carryEventBus.post(tempQueueInitializeEvent); carryEventBus.post(msgBodyCompressEvent); carryEventBus.post(requestEvent); carryEventBus.post(tagGenerateEvent); carryEventBus.post(blockAndTimeoutResponseEvent); //unregister carryEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/GenericResponser.java ================================================ package com.messagebus.client.carry; import com.google.common.eventbus.EventBus; import com.messagebus.client.IRequestListener; import com.messagebus.client.MessageContext; import com.messagebus.client.event.carry.CommonEventProcessor; import com.messagebus.client.event.carry.ResponseEventProcessor; import com.messagebus.client.model.MessageCarryType; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; class GenericResponser extends AbstractMessageCarryer implements IResponser { private static final Log logger = LogFactory.getLog(GenericResponser.class); @Override public void response(String secret, IRequestListener onRequest, long timeout, TimeUnit timeUnit) { final MessageContext ctx = initMessageContext(); ctx.setCarryType(MessageCarryType.RESPONSE); ctx.setSecret(secret); ctx.setSink(this.getContext().getConfigManager().getSinkBySecret(secret)); ctx.setRequestListener(onRequest); ctx.setTimeout(timeout); ctx.setTimeoutUnit(timeUnit); this.innerResponse(ctx); } private void innerResponse(MessageContext ctx) { EventBus carryEventBus = this.getContext().getCarryEventBus(); //register event processor ResponseEventProcessor eventProcessor = new ResponseEventProcessor(); carryEventBus.register(eventProcessor); CommonEventProcessor.TagGenerateEvent tagGenerateEvent = new CommonEventProcessor.TagGenerateEvent(); ResponseEventProcessor.ValidateEvent validateEvent = new ResponseEventProcessor.ValidateEvent(); ResponseEventProcessor.PermissionCheckEvent permissionCheckEvent = new ResponseEventProcessor.PermissionCheckEvent(); CommonEventProcessor.PreConsumeEvent perConsumeEvent = new CommonEventProcessor.PreConsumeEvent(); CommonEventProcessor.AsyncMessageLoopEvent asyncMessageLoopEvent = new CommonEventProcessor.AsyncMessageLoopEvent(); tagGenerateEvent.setMessageContext(ctx); validateEvent.setMessageContext(ctx); permissionCheckEvent.setMessageContext(ctx); perConsumeEvent.setMessageContext(ctx); asyncMessageLoopEvent.setMessageContext(ctx); //arrange event order and emit carryEventBus.post(tagGenerateEvent); carryEventBus.post(validateEvent); carryEventBus.post(permissionCheckEvent); carryEventBus.post(perConsumeEvent); carryEventBus.post(asyncMessageLoopEvent); //unregister carryEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/GenericRpcRequester.java ================================================ package com.messagebus.client.carry; import com.google.common.eventbus.EventBus; import com.messagebus.client.MessageContext; import com.messagebus.client.event.carry.RpcRequestEventProcessor; import com.messagebus.client.model.MessageCarryType; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.Map; /** * Created by yanghua on 4/8/15. */ public class GenericRpcRequester extends AbstractMessageCarryer implements IRpcRequester { private static final Log logger = LogFactory.getLog(GenericRpcRequester.class); public GenericRpcRequester() { } @Override public Object call(String secret, String target, String methodName, Object[] params, String token, long timeoutOfMilliSecond) { MessageContext ctx = initMessageContext(); ctx.setSecret(secret); ctx.setToken(token); ctx.setCarryType(MessageCarryType.RPCREQUEST); ctx.setSource(this.getContext().getConfigManager().getSourceBySecret(secret)); ctx.setTimeout(timeoutOfMilliSecond); ctx.setSink(this.getContext().getConfigManager().getSinkByName(target)); ctx.setStream(this.getContext().getConfigManager().getStreamByToken(token)); Map otherParams = ctx.getOtherParams(); otherParams.put("methodName", methodName); otherParams.put("params", params); this.innerRpcRequest(ctx); return otherParams.get("result"); } private void innerRpcRequest(MessageContext ctx) { EventBus carryEventBus = this.getContext().getCarryEventBus(); //register event processor RpcRequestEventProcessor eventProcessor = new RpcRequestEventProcessor(); carryEventBus.register(eventProcessor); RpcRequestEventProcessor.PermissionCheckEvent permissionCheckEvent = new RpcRequestEventProcessor.PermissionCheckEvent(); RpcRequestEventProcessor.RpcRequestEvent rpcRequestEvent = new RpcRequestEventProcessor.RpcRequestEvent(); permissionCheckEvent.setMessageContext(ctx); rpcRequestEvent.setMessageContext(ctx); carryEventBus.post(permissionCheckEvent); carryEventBus.post(rpcRequestEvent); //unregister carryEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/GenericRpcResponser.java ================================================ package com.messagebus.client.carry; import com.google.common.eventbus.EventBus; import com.messagebus.client.ConfigManager; import com.messagebus.client.IRpcMessageProcessor; import com.messagebus.client.MessageContext; import com.messagebus.client.WrappedRpcServer; import com.messagebus.client.event.carry.RpcResponseEventProcessor; import com.messagebus.client.model.MessageCarryType; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.QueueingConsumer; import com.rabbitmq.client.RpcServer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 4/8/15. */ public class GenericRpcResponser extends AbstractMessageCarryer implements IRpcResponser { private static final Log logger = LogFactory.getLog(GenericRpcResponser.class); public GenericRpcResponser() { } @Override public void callback(String secret, Class clazzOfInterface, Object serviceProvider, long timeout, TimeUnit timeUnit) { MessageContext ctx = initMessageContext(); ctx.setSecret(secret); ctx.setCarryType(MessageCarryType.RPCRESPONSE); ctx.setSink(this.getContext().getConfigManager().getSinkBySecret(secret)); Map otherParams = ctx.getOtherParams(); otherParams.put("serviceProvider", serviceProvider); otherParams.put("clazzOfInterface", clazzOfInterface); ctx.setTimeout(timeout); ctx.setTimeoutUnit(timeUnit); this.innerRpcResponse(ctx); } @Override public WrappedRpcServer buildRpcServer(String secret, final IRpcMessageProcessor rpcMsgProcessor) { ConfigManager.Sink sink = this.getContext().getConfigManager().getSinkBySecret(secret); try { RpcServer aServer = new RpcServer(this.getContext().getChannel(), sink.getQueueName()) { @Override public byte[] handleCall(QueueingConsumer.Delivery request, AMQP.BasicProperties replyProperties) { return rpcMsgProcessor.onRpcMessage(request.getBody()); } }; Constructor rpcServerConstructor = WrappedRpcServer.class.getDeclaredConstructor(RpcServer.class); rpcServerConstructor.setAccessible(true); WrappedRpcServer wrappedRpcServer = rpcServerConstructor.newInstance(aServer); rpcServerConstructor.setAccessible(false); return wrappedRpcServer; } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } catch (NoSuchMethodException e) { logger.error(e); throw new RuntimeException(e); } catch (InstantiationException e) { logger.error(e); throw new RuntimeException(e); } catch (IllegalAccessException e) { logger.error(e); throw new RuntimeException(e); } catch (InvocationTargetException e) { logger.error(e); throw new RuntimeException(e); } catch (Exception e) { logger.error(e); throw new RuntimeException(e); } } private void innerRpcResponse(MessageContext ctx) { EventBus carryEventBus = this.getContext().getCarryEventBus(); //register event processor RpcResponseEventProcessor eventProcessor = new RpcResponseEventProcessor(); carryEventBus.register(eventProcessor); RpcResponseEventProcessor.RpcResponseEvent rpcResponseEvent = new RpcResponseEventProcessor.RpcResponseEvent(); rpcResponseEvent.setMessageContext(ctx); carryEventBus.post(rpcResponseEvent); carryEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/GenericSubscriber.java ================================================ package com.messagebus.client.carry; import com.google.common.eventbus.EventBus; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.MessageContext; import com.messagebus.client.event.carry.CommonEventProcessor; import com.messagebus.client.event.carry.SubscribeEventProcessor; import com.messagebus.client.model.MessageCarryType; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; class GenericSubscriber extends AbstractMessageCarryer implements ISubscriber { private static final Log logger = LogFactory.getLog(GenericSubscriber.class); public GenericSubscriber() { } @Override public void subscribe(String secret, String from, String token, IMessageReceiveListener onMessage, long timeout, TimeUnit unit) { final MessageContext ctx = initMessageContext(); ctx.setCarryType(MessageCarryType.SUBSCRIBE); ctx.setSecret(secret); ctx.setToken(token); ctx.setSource(this.getContext().getConfigManager().getSourceByName(from)); ctx.setSink(this.getContext().getConfigManager().getSinkBySecret(secret)); ctx.setStream(this.getContext().getConfigManager().getStreamByToken(token)); ctx.setReceiveListener(onMessage); ctx.setSync(false); ctx.setTimeout(timeout); ctx.setTimeoutUnit(unit); this.innerSubscribe(ctx); } private void innerSubscribe(MessageContext ctx) { EventBus carryEventBus = this.getContext().getCarryEventBus(); //register event processor SubscribeEventProcessor eventProcessor = new SubscribeEventProcessor(); carryEventBus.register(eventProcessor); //init events CommonEventProcessor.TagGenerateEvent tagGenerateEvent = new CommonEventProcessor.TagGenerateEvent(); SubscribeEventProcessor.ValidateEvent validateEvent = new SubscribeEventProcessor.ValidateEvent(); SubscribeEventProcessor.PermissionCheckEvent permissionCheckEvent = new SubscribeEventProcessor.PermissionCheckEvent(); CommonEventProcessor.PreConsumeEvent perConsumeEvent = new CommonEventProcessor.PreConsumeEvent(); CommonEventProcessor.AsyncMessageLoopEvent asyncMessageLoopEvent = new CommonEventProcessor.AsyncMessageLoopEvent(); tagGenerateEvent.setMessageContext(ctx); validateEvent.setMessageContext(ctx); permissionCheckEvent.setMessageContext(ctx); perConsumeEvent.setMessageContext(ctx); asyncMessageLoopEvent.setMessageContext(ctx); carryEventBus.post(tagGenerateEvent); carryEventBus.post(validateEvent); carryEventBus.post(permissionCheckEvent); carryEventBus.post(perConsumeEvent); carryEventBus.post(asyncMessageLoopEvent); carryEventBus.unregister(eventProcessor); } } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/IBroadcaster.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.message.model.Message; public interface IBroadcaster { public void broadcast(String secret, Message[] msgs); } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/IConsumer.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.message.model.Message; import java.util.List; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 3/1/15. */ public interface IConsumer { public void consume(String secret, long timeout, TimeUnit unit, IMessageReceiveListener onMessage); public List consume(String secret, int expectedNum); } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/IProducer.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.message.model.Message; /** * the interface of producer */ public interface IProducer { public void produce(String secret, String to, Message msg, String token); public void batchProduce(String secret, String to, Message[] msgs, String token); } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/IPublisher.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.message.model.Message; public interface IPublisher { public void publish(String secret, Message[] msgs); } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/IRequester.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.MessageResponseTimeoutException; import com.messagebus.client.message.model.Message; public interface IRequester { public Message request(String secret, String to, Message msg, String token, long timeout) throws MessageResponseTimeoutException; public byte[] primitiveRequest(String secret, String target, byte[] requestMsg, String token, long timeoutOfMilliSecond); } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/IResponser.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.IRequestListener; import java.util.concurrent.TimeUnit; public interface IResponser { /** * response a temp message to a named queue * * @param toTmpQueue the temp queue name * @param msg the entity of message * @param receiveListener * @param secret * @param onRequest * @param timeout * @param timeUnit */ public void response(String secret, IRequestListener onRequest, long timeout, TimeUnit timeUnit); } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/IRpcRequester.java ================================================ package com.messagebus.client.carry; /** * Created by yanghua on 4/8/15. */ public interface IRpcRequester { public Object call(String secret, String target, String methodName, Object[] params, String token, long timeoutOfMilliSecond); } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/IRpcResponser.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.IRpcMessageProcessor; import com.messagebus.client.WrappedRpcServer; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 4/8/15. */ public interface IRpcResponser { public void callback(String secret, Class clazzOfInterface, Object serviceProvider, long timeout, TimeUnit timeUnit); public WrappedRpcServer buildRpcServer(String secret, IRpcMessageProcessor rpcMsgProcessor); } ================================================ FILE: client/src/main/java/com/messagebus/client/carry/ISubscriber.java ================================================ package com.messagebus.client.carry; import com.messagebus.client.IMessageReceiveListener; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 3/1/15. */ public interface ISubscriber { public void subscribe(String secret, String from, String token, IMessageReceiveListener onMessage, long timeout, TimeUnit unit); } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/BroadcastEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.base.Strings; import com.google.common.eventbus.Subscribe; import com.messagebus.client.ConfigManager; import com.messagebus.client.MessageContext; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.transfer.MessageHeaderTransfer; import com.messagebus.client.model.MessageCarryType; import com.messagebus.common.Constants; import com.messagebus.interactor.proxy.ProxyProducer; import com.rabbitmq.client.AMQP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * Created by yanghua on 6/29/15. */ public class BroadcastEventProcessor extends CommonEventProcessor { private static final Log logger = LogFactory.getLog(BroadcastEventProcessor.class); private static final String EVENT_ROUTING_KEY_NAME = "routingkey.proxy.message.inner.#"; @Subscribe public void onValidate(ValidateEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onValidate =-=-=-"); super.onValidate(event); MessageContext context = event.getMessageContext(); if (!context.getCarryType().equals(MessageCarryType.BROADCAST)) { logger.error("the message carry type should be broadcast"); event.getMessageContext().setThrowable(new RuntimeException( "the message carry type should be broadcast")); } this.validateMessageProperties(context); } @Subscribe public void onPermissionCheck(PermissionCheckEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onPermissionCheck =-=-=-"); boolean hasPermission; MessageContext context = event.getMessageContext(); ConfigManager.Source source = context.getSource(); hasPermission = source.getBroadcastable().equals("1"); if (!hasPermission) { logger.error("the source with name : " + source.getName() + ", with secret : " + source.getSecret() + " can not broadcast !"); event.getMessageContext().setThrowable(new RuntimeException("the queue with name : " + source.getName() + ", with secret : " + source.getSecret() + " can not broadcast !")); } } @Subscribe public void onBroadcast(BroadcastEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onBroadcast =-=-=-"); MessageContext context = event.getMessageContext(); try { for (Message msg : context.getMessages()) { Map map = new HashMap(1); map.put("type", "notice"); msg.setHeaders(map); AMQP.BasicProperties properties = MessageHeaderTransfer.box(msg); ProxyProducer.produce(Constants.PROXY_EXCHANGE_NAME, context.getChannel(), EVENT_ROUTING_KEY_NAME, msg.getContent(), properties); } } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable( new RuntimeException(e)); } } @Override public void process(MessageContext msgContext) { throw new UnsupportedOperationException("this method should be implemented in consume event processor!"); } private void validateMessageProperties(MessageContext context) { Date currentDate = new Date(); for (Message msg : context.getMessages()) { //app id if (Strings.isNullOrEmpty(msg.getAppId())) msg.setAppId(context.getSource().getAppId()); //timestamp if (msg.getTimestamp() == 0) msg.setTimestamp(currentDate.getTime()); } } public static class ValidateEvent extends CarryEvent { } public static class PermissionCheckEvent extends CarryEvent { } public static class BroadcastEvent extends CarryEvent { } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/CarryEvent.java ================================================ package com.messagebus.client.event.carry; import com.messagebus.client.MessageContext; import com.messagebus.common.Event; /** * Created by yanghua on 6/25/15. */ public class CarryEvent extends Event { private MessageContext messageContext; public MessageContext getMessageContext() { return messageContext; } public void setMessageContext(MessageContext messageContext) { this.messageContext = messageContext; } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/CommonEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.base.Strings; import com.google.common.eventbus.Subscribe; import com.messagebus.client.ConfigManager; import com.messagebus.client.MessageContext; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import com.messagebus.common.RandomHelper; import com.messagebus.common.UUIDGenerator; import com.messagebus.interactor.proxy.ProxyConsumer; import com.rabbitmq.client.QueueingConsumer; import com.rabbitmq.client.ShutdownSignalException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.Random; import java.util.concurrent.*; /** * Created by yanghua on 6/25/15. */ public abstract class CommonEventProcessor { private static final Log logger = LogFactory.getLog(CommonEventProcessor.class); private static final Random random = new Random(); protected void onValidate(CarryEvent event) { //todo } protected void exceptionCheck(CarryEvent event) { if (event.getMessageContext().getThrowable() != null) return; } @Subscribe public void onMsgIdGenerate(MsgIdGenerateEvent event) { this.exceptionCheck(event); logger.debug("=-=-=- event : onMsgIdGenerate =-=-=-"); MessageContext context = event.getMessageContext(); Message[] msgs = context.getMessages(); for (Message msg : msgs) { UUIDGenerator generator = new UUIDGenerator(random.nextInt(31), Constants.DEFAULT_DATACENTER_ID_FOR_UUID); logger.debug("message id is : " + generator.nextId()); msg.setMessageId(generator.nextId()); msg.setCorrelationId(context.getSource().getName()); } } @Subscribe public void onMsgBodySizeCheck(MsgBodySizeCheckEvent event) { this.exceptionCheck(event); logger.debug("=-=-=- event : onMsgBodySizeCheck =-=-=-"); MessageContext context = event.getMessageContext(); ConfigManager.Sink sink = context.getSink(); if (sink != null && !Strings.isNullOrEmpty(sink.getMsgBodySize())) { String msgBodySizeStr = sink.getMsgBodySize(); int msgBodySize = Integer.parseInt(msgBodySizeStr); if (msgBodySize != -1) { Message[] msgs = context.getMessages(); for (Message msg : msgs) { if (msg.getContent().length > msgBodySize) { logger.error("message body's size can not be more than : " + msgBodySizeStr + " B, the limit comes from queue name : " + sink.getName()); event.getMessageContext().setThrowable( new RuntimeException("message body's size can not be more than : " + msgBodySizeStr + " B")); return; } } } } } @Subscribe public void onTagGenerate(TagGenerateEvent event) { this.exceptionCheck(event); logger.debug("=-=-=- event : onTagGenerate =-=-=-"); MessageContext context = event.getMessageContext(); String tag = "consumer.tag." + RandomHelper.randomNumberAndCharacter(6); context.setConsumerTag(tag); } @Subscribe public void onMsgBodyCompress(MsgBodyCompressEvent event) { this.exceptionCheck(event); //TODO : 不提供自定义功能,有总线决定压缩方式 // logger.debug("=-=-=- event : onMsgBodyCompress =-=-=-"); // MessageContext context = event.getMessageContext(); // String compressAlgo = context.getSink().getCompress(); // if (!Strings.isNullOrEmpty(compressAlgo)) { // ICompressor compressor = CompressorFactory.createCompressor(compressAlgo); // if (compressor != null) { // for (Message msg : context.getMessages()) { // if (msg.getHeaders() == null) msg.setHeaders(new HashMap(1)); // msg.getHeaders().put(Constants.MESSAGE_HEADER_KEY_COMPRESS_ALGORITHM, compressAlgo); // msg.setContent(compressor.compress(msg.getContent())); // } // } else { // logger.error("the target node with name : " + context.getSink().getName() // + " configured a compress named : " + compressAlgo // + " but client can not get the compressor instance. "); // } // } } @Subscribe public void onPreConsume(PreConsumeEvent event) { this.exceptionCheck(event); logger.debug("=-=-=- event : onPreConsume =-=-=-"); MessageContext context = event.getMessageContext(); if (!context.isSync()) { QueueingConsumer consumer = null; try { boolean autoAck = context.getSink().isAutoAck(); consumer = ProxyConsumer.consume(context.getChannel(), context.getSink().getQueueName(), autoAck, context.getConsumerTag()); } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable( new RuntimeException(e)); return; } //add external params context.getOtherParams().put("consumer", consumer); } } @Subscribe public void onAsyncMessageLoop(AsyncMessageLoopEvent event) { this.exceptionCheck(event); logger.debug("=-=-=- event : onAsyncMessageLoop =-=-=-"); MessageContext context = event.getMessageContext(); ExecutorService executor = Executors.newFixedThreadPool(1); Future messageLoopTask = executor.submit(new MessageLoopTask(context)); //block until interrupt or timeout try { messageLoopTask.get(context.getTimeout(), context.getTimeoutUnit()); } catch (InterruptedException e) { logger.info(" consume interrupted!"); } catch (ExecutionException e) { logger.error(e); event.getMessageContext().setThrowable(e); } catch (TimeoutException e) { logger.info("message loop timeout after : " + context.getTimeout() + " [" + context.getTimeoutUnit() + "]"); event.getMessageContext().setThrowable(e); } finally { //close the consume based on this channel synchronized (context.getChannel()) { try { if (context.getChannel().isOpen()) { context.getChannel().basicCancel(context.getConsumerTag()); } } catch (IOException e1) { logger.error(e1); event.getMessageContext().setThrowable(e1); } } } } public abstract void process(MessageContext msgContext); public static class MsgIdGenerateEvent extends CarryEvent { } public static class TagGenerateEvent extends CarryEvent { } public static class PreConsumeEvent extends CarryEvent { } public static class MsgBodyCompressEvent extends CarryEvent { } public static class MsgBodySizeCheckEvent extends CarryEvent { } public static class AsyncMessageLoopEvent extends CarryEvent { } /** * another message loop task for consume and process message */ private class MessageLoopTask implements Runnable { private MessageContext msgContext; public MessageLoopTask(MessageContext msgContext) { this.msgContext = msgContext; } @Override public void run() { QueueingConsumer currentConsumer = (QueueingConsumer) msgContext.getOtherParams().get("consumer"); int retryCount = 0, retryTotalCount = 10; boolean stop = false; try { while (!stop) { try { QueueingConsumer.Delivery delivery = currentConsumer.nextDelivery(); final Message msg = MessageFactory.createMessage(delivery); if (msg == null) continue; doUncompress(msgContext, msg); msgContext.setConsumeMsgs(new ArrayList(1) {{ this.add(msg); }}); process(msgContext); if (!msgContext.getSink().isAutoAck()) { msgContext.getChannel().basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } catch (InterruptedException e) { logger.info(" message loop task interrupted!"); } catch (ShutdownSignalException e) { retryCount++; if (retryCount >= retryTotalCount) stop = true; } catch (Exception e) { logger.error(e); } } } catch (Exception e) { logger.error(e); } } } protected void validateMessagesProperties(MessageContext context) { Date currentDate = new Date(); for (Message msg : context.getMessages()) { //app id if (msg.getAppId() == null || msg.getAppId().isEmpty()) msg.setAppId(context.getSource().getAppId()); //timestamp if (msg.getTimestamp() == 0) msg.setTimestamp(currentDate.getTime()); } } protected void doUncompress(MessageContext context, Message receivedMsg) { //TODO : 不提供自定义功能,有总线决定压缩方式 // String compressAlgo = context.getSource().getCompress(); // if (!Strings.isNullOrEmpty(compressAlgo)) { // ICompressor compressor = CompressorFactory.createCompressor(compressAlgo); // if (compressor != null) { // receivedMsg.setContent(compressor.uncompress(receivedMsg.getContent())); // } else { // logger.error("the target node with name : " + context.getSink().getName() // + " configured a compress named : " + compressAlgo // + " but client can not get the compressor instance. "); // } // } } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/ConsumeEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.base.Strings; import com.google.common.eventbus.Subscribe; import com.messagebus.client.ConfigManager; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.MessageContext; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.client.model.MessageCarryType; import com.messagebus.interactor.proxy.ProxyConsumer; import com.rabbitmq.client.GetResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by yanghua on 6/26/15. */ public class ConsumeEventProcessor extends CommonEventProcessor { private static final Log logger = LogFactory.getLog(ConsumeEventProcessor.class); @Subscribe public void onValidate(ValidateEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onValidate =-=-=-"); super.onValidate(event); } @Subscribe public void onPermissionCheck(PermissionCheckEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onPermissionCheck =-=-=-"); MessageContext context = event.getMessageContext(); ConfigManager.Sink sink = context.getSink(); boolean hasPermission = false; hasPermission = MessageCarryType.lookup(sink.getType()).equals(MessageCarryType.CONSUME) && !Strings.isNullOrEmpty(sink.getQueueName()) && !Strings.isNullOrEmpty(sink.getName()); if (!hasPermission) { logger.error("permission error : can not consume. may be communicate type is wrong. " + " current secret is : " + sink.getSecret()); event.getMessageContext().setThrowable( new RuntimeException("permission error : can not consume. may be communicate type is wrong. " + " current secret is : " + sink.getSecret())); } } @Subscribe public void onSyncConsume(SyncConsumeEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onSyncConsume =-=-=-"); MessageContext context = event.getMessageContext(); if (context.isSync()) { List consumeMsgs = new ArrayList(context.getConsumeMsgNum()); context.setConsumeMsgs(consumeMsgs); try { int countDown = context.getConsumeMsgNum(); while (countDown-- > 0) { GetResponse response = ProxyConsumer.consumeSingleMessage(context.getChannel(), context.getSink().getQueueName()); if (response == null) continue; final Message msg = MessageFactory.createMessage(response); if (msg == null) continue; this.doUncompress(context, msg); consumeMsgs.add(msg); } } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable(e); } catch (RuntimeException e) { logger.error(e); event.getMessageContext().setThrowable(e); } } } @Override public void process(MessageContext msgContext) { IMessageReceiveListener receiveListener = msgContext.getReceiveListener(); receiveListener.onMessage(msgContext.getConsumeMsgs().get(0)); } public static class ValidateEvent extends CarryEvent { } public static class PermissionCheckEvent extends CarryEvent { } public static class SyncConsumeEvent extends CarryEvent { } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/ProduceEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.eventbus.Subscribe; import com.messagebus.client.ConfigManager; import com.messagebus.client.MessageContext; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.transfer.MessageHeaderTransfer; import com.messagebus.client.model.MessageCarryType; import com.messagebus.common.Constants; import com.messagebus.interactor.proxy.ProxyProducer; import com.rabbitmq.client.AMQP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; /** * Created by yanghua on 6/25/15. */ public class ProduceEventProcessor extends CommonEventProcessor { private static final Log logger = LogFactory.getLog(ProduceEventProcessor.class); public ProduceEventProcessor() { } @Subscribe public void onValidate(ValidateEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onValidate =-=-=-"); super.onValidate(event); MessageContext context = event.getMessageContext(); ConfigManager.Source source = context.getSource(); if (!context.getCarryType().equals(MessageCarryType.PRODUCE)) { logger.error("message carry type should be produce, but current is : " + context.getCarryType() + "; node name : " + source.getName() + "; node secret is : " + source.getSecret()); event.getMessageContext().setThrowable(new RuntimeException("message carry type should be produce, but current is : " + context.getCarryType() + "; node name : " + source.getName() + "; node secret is : " + source.getSecret())); return; } if (context.getSink() == null) { logger.error("target node can not be null " + "; node name : " + source.getName() + "; node secret is : " + source.getSecret()); event.getMessageContext().setThrowable(new RuntimeException("target node can not be null " + "; node name : " + source.getName() + "; node secret is : " + source.getSecret())); return; } if (!MessageCarryType.lookup(context.getSink().getType()).equals(MessageCarryType.CONSUME)) { logger.error("target node's type is illegal , " + "target node name : " + context.getSink().getName()); event.getMessageContext().setThrowable(new RuntimeException("target node's type is illegal , " + "target node name : " + context.getSink().getName())); return; } this.validateMessagesProperties(context); } @Subscribe public void onPermissionCheckEvent(PermissionCheckEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onPermissionCheckEvent =-=-=-"); MessageContext context = event.getMessageContext(); ConfigManager.Source source = context.getSource(); ConfigManager.Sink sink = context.getSink(); boolean hasPermission = false; ConfigManager.Stream stream = context.getStream(); hasPermission = stream.getSourceName().equals(source.getName()) && stream.getSinkSecret().equals(sink.getSecret()); if (!hasPermission) { logger.error("[handle] can not produce message from queue [" + source.getName() + "] to queue [" + sink.getName() + "]"); event.getMessageContext().setThrowable(new RuntimeException("can not produce message from queue [" + source.getName() + "] to queue [" + sink.getName() + "]")); return; } } @Subscribe public void onProduce(ProduceEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onProduce =-=-=-"); MessageContext context = event.getMessageContext(); try { if (context.isEnableTransaction()) { for (Message msg : context.getMessages()) { AMQP.BasicProperties properties = MessageHeaderTransfer.box(msg); ProxyProducer.produceWithTX(Constants.PROXY_EXCHANGE_NAME, context.getChannel(), context.getSink().getRoutingKey(), msg.getContent(), properties); } } else { for (Message msg : context.getMessages()) { AMQP.BasicProperties properties = MessageHeaderTransfer.box(msg); ProxyProducer.produce(Constants.PROXY_EXCHANGE_NAME, context.getChannel(), context.getSink().getRoutingKey(), msg.getContent(), properties); } } } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable(e); } catch (Exception e) { logger.error(e); event.getMessageContext().setThrowable(e); } } @Override public void process(MessageContext msgContext) { throw new UnsupportedOperationException("this method should be implemented in consume event processor!"); } //region events definition public static class ValidateEvent extends CarryEvent { } public static class PermissionCheckEvent extends CarryEvent { } public static class ProduceEvent extends CarryEvent { } //endregion } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/PublishEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.eventbus.Subscribe; import com.messagebus.client.ConfigManager; import com.messagebus.client.MessageContext; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.transfer.MessageHeaderTransfer; import com.messagebus.client.model.MessageCarryType; import com.messagebus.common.Constants; import com.messagebus.interactor.proxy.ProxyProducer; import com.rabbitmq.client.AMQP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; /** * Created by yanghua on 6/26/15. */ public class PublishEventProcessor extends CommonEventProcessor { private static final Log logger = LogFactory.getLog(PublishEventProcessor.class); public PublishEventProcessor() { } @Subscribe public void onValidate(ValidateEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onValidate =-=-=-"); super.onValidate(event); MessageContext context = event.getMessageContext(); if (!context.getCarryType().equals(MessageCarryType.PUBLISH)) { logger.error("message carry type should be publish "); event.getMessageContext().setThrowable(new RuntimeException("message carry type should be publish ")); return; } this.validateMessagesProperties(context); } @Subscribe public void onPermissionCheck(PermissionCheckEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onPermissionCheck =-=-=-"); MessageContext context = event.getMessageContext(); ConfigManager.Source source = context.getSource(); boolean hasPermission = false; hasPermission = MessageCarryType.lookup(source.getType()).equals(MessageCarryType.PUBLISH); if (!hasPermission) { logger.error("can not publish message! maybe the communicate is error. " + " secret is : " + context.getSecret()); event.getMessageContext().setThrowable(new RuntimeException("can not publish message! maybe the communicate is error. " + " secret is : " + context.getSecret())); return; } } @Subscribe public void onPublish(PublishEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onPublish =-=-=-"); MessageContext context = event.getMessageContext(); try { for (Message msg : context.getMessages()) { AMQP.BasicProperties properties = MessageHeaderTransfer.box(msg); ProxyProducer.produce(Constants.PROXY_EXCHANGE_NAME, context.getChannel(), context.getSource().getRoutingKey(), msg.getContent(), properties); } } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable(e); } catch (Exception e) { logger.error(e); event.getMessageContext().setThrowable(e); } } @Override public void process(MessageContext msgContext) { throw new UnsupportedOperationException("this method should be implemented in consume event processor!"); } //region publish events definition public static class ValidateEvent extends CarryEvent { } public static class PermissionCheckEvent extends CarryEvent { } public static class PublishEvent extends CarryEvent { } //endregion } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/RequestEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.eventbus.Subscribe; import com.messagebus.client.ConfigManager; import com.messagebus.client.MessageContext; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.client.message.transfer.MessageHeaderTransfer; import com.messagebus.client.model.MessageCarryType; import com.messagebus.common.Constants; import com.messagebus.interactor.proxy.ProxyConsumer; import com.messagebus.interactor.proxy.ProxyProducer; import com.messagebus.interactor.rabbitmq.QueueManager; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.QueueingConsumer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.ArrayList; /** * Created by yanghua on 6/27/15. */ public class RequestEventProcessor extends CommonEventProcessor { private static final Log logger = LogFactory.getLog(RequestEventProcessor.class); public RequestEventProcessor() { } @Subscribe public void onValidate(ValidateEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onValidate =-=-=-"); super.onValidate(event); MessageContext context = event.getMessageContext(); if (!context.getCarryType().equals(MessageCarryType.REQUEST)) { logger.error("message carry type should be request"); event.getMessageContext().setThrowable( new RuntimeException("message carry type should be request")); return; } if (!MessageCarryType.lookup(context.getSource().getType()).equals(MessageCarryType.REQUEST)) { logger.error("message carry type should be request"); event.getMessageContext().setThrowable( new RuntimeException("message carry type should be request")); return; } Message[] msgs = context.getMessages(); if (msgs == null || msgs.length != 1) { logger.error(" request message must be just one! "); event.getMessageContext().setThrowable( new RuntimeException(" request message must be just one! ")); return; } if (context.getSink() == null) { logger.error(" sink can not be null. "); event.getMessageContext().setThrowable( new RuntimeException(" sink can not be null. ")); return; } this.validateMessagesProperties(context); } @Subscribe public void onPermissionCheck(PermissionCheckEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onPermissionCheck =-=-=-"); MessageContext context = event.getMessageContext(); ConfigManager.Source source = context.getSource(); ConfigManager.Sink sink = context.getSink(); String token = context.getToken(); boolean hasPermission = false; ConfigManager.Stream stream = context.getStream(); hasPermission = stream != null && stream.getToken().equals(token) && stream.getSourceSecret().equals(source.getSecret()) && stream.getSinkSecret().equals(sink.getSecret()); if (!hasPermission) { logger.error("[handle] can not produce message from queue [" + source.getName() + "] to queue [" + sink.getName() + "]"); event.getMessageContext().setThrowable(new RuntimeException("can not produce message from queue [" + source.getName() + "] to queue [" + sink.getName() + "]")); return; } } @Subscribe public void onTempQueueInitialize(TempQueueInitializeEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onTempQueueInitialize =-=-=-"); MessageContext context = event.getMessageContext(); String correlationId = context.getSource().getName(); context.getMessages()[0].setCorrelationId(correlationId); QueueManager queueManager = QueueManager.defaultQueueManager(context.getHost()); try { queueManager.create(correlationId); } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable(new RuntimeException(e)); } catch (Exception e) { logger.error(e); event.getMessageContext().setThrowable(new RuntimeException(e)); } } @Subscribe public void onRequest(RequestEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onRequest =-=-=-"); MessageContext context = event.getMessageContext(); Message reqMsg = context.getMessages()[0]; AMQP.BasicProperties properties = MessageHeaderTransfer.box(reqMsg); try { ProxyProducer.produceWithTX(Constants.PROXY_EXCHANGE_NAME, context.getChannel(), context.getSink().getRoutingKey(), reqMsg.getContent(), properties); } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable(new RuntimeException(e)); } catch (Exception e) { logger.error(e); event.getMessageContext().setThrowable(new RuntimeException(e)); } } @Subscribe public void onBlockAndTimeout(BlockAndTimeoutResponseEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onBlockAndTimeout =-=-=-"); MessageContext context = event.getMessageContext(); String correlationId = context.getMessages()[0].getCorrelationId(); try { //just receive one QueueingConsumer consumer = ProxyConsumer.consume( context.getChannel(), correlationId, true, context.getConsumerTag()); QueueingConsumer.Delivery delivery = consumer.nextDelivery(context.getTimeout() * 1000); //timeout if (delivery == null) { context.setIsTimeout(true); return; } final Message msg = MessageFactory.createMessage(delivery); if (msg == null) return; context.setConsumeMsgs(new ArrayList(1) {{ this.add(msg); }}); } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable(e); } catch (InterruptedException e) { } finally { //delete temp queue QueueManager queueManager = QueueManager.defaultQueueManager(context.getHost()); String msgIdStr = String.valueOf(correlationId); try { if (queueManager.exists(msgIdStr)) { queueManager.delete(msgIdStr); } } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable(e); } } } @Override public void process(MessageContext msgContext) { throw new UnsupportedOperationException("this method should be implemented in consume event processor!"); } //region event definition public static class ValidateEvent extends CarryEvent { } public static class PermissionCheckEvent extends CarryEvent { } public static class TempQueueInitializeEvent extends CarryEvent { } public static class RequestEvent extends CarryEvent { } public static class BlockAndTimeoutResponseEvent extends CarryEvent { } //endregion } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/ResponseEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.eventbus.Subscribe; import com.messagebus.client.ConfigManager; import com.messagebus.client.IRequestListener; import com.messagebus.client.MessageContext; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.transfer.MessageHeaderTransfer; import com.messagebus.client.model.MessageCarryType; import com.messagebus.interactor.proxy.ProxyProducer; import com.rabbitmq.client.AMQP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; /** * Created by yanghua on 6/29/15. */ public class ResponseEventProcessor extends CommonEventProcessor { private static final Log logger = LogFactory.getLog(ResponseEventProcessor.class); public ResponseEventProcessor() { } @Subscribe public void onValidate(ValidateEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onValidate =-=-=-"); super.onValidate(event); MessageContext context = event.getMessageContext(); if (!context.getCarryType().equals(MessageCarryType.RESPONSE)) { logger.error(" message carry type should be response "); event.getMessageContext().setThrowable( new RuntimeException("message carry type should be response ")); } } @Subscribe public void onPermissionCheck(PermissionCheckEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onPermissionCheck =-=-=-"); MessageContext context = event.getMessageContext(); ConfigManager.Sink sink = context.getSink(); boolean hasPermission = MessageCarryType.lookup(sink.getType()).equals(MessageCarryType.RESPONSE); if (!hasPermission) { logger.error("permission error : can not response. " + "may be communicate type is wrong. " + "current secret is : " + sink.getSecret()); event.getMessageContext().setThrowable(new RuntimeException("permission error : can not response. " + "may be communicate type is wrong. " + "current secret is : " + sink.getSecret())); } } @Override public void process(MessageContext msgContext) { IRequestListener requestListener = msgContext.getRequestListener(); Message requestMsg = msgContext.getConsumeMsgs().get(0); String tempQueueName = requestMsg.getCorrelationId(); msgContext.setTempQueueName(tempQueueName); Message respMsg = requestListener.onRequest(msgContext.getConsumeMsgs().get(0)); AMQP.BasicProperties properties = MessageHeaderTransfer.box(respMsg); try { ProxyProducer.produce("", msgContext.getChannel(), tempQueueName, respMsg.getContent(), properties); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } catch (Exception e) { logger.error(e); throw new RuntimeException(e); } } public static class ValidateEvent extends CarryEvent { } public static class PermissionCheckEvent extends CarryEvent { } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/RpcRequestEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.eventbus.Subscribe; import com.messagebus.client.ConfigManager; import com.messagebus.client.MessageContext; import com.messagebus.common.Constants; import com.rabbitmq.tools.jsonrpc.JsonRpcClient; import com.rabbitmq.tools.jsonrpc.JsonRpcException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; /** * Created by yanghua on 6/29/15. */ public class RpcRequestEventProcessor extends CommonEventProcessor { private static final Log logger = LogFactory.getLog(RpcRequestEventProcessor.class); public RpcRequestEventProcessor() { } @Subscribe public void onPermissionCheck(PermissionCheckEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onPermissionCheck =-=-=-"); MessageContext context = event.getMessageContext(); ConfigManager.Source source = context.getSource(); ConfigManager.Sink sink = context.getSink(); boolean hasPermission = false; hasPermission = context.getStream() != null && context.getStream().getSinkSecret().equals(sink.getSecret()) && context.getStream().getSourceName().equals(source.getName()); if (!hasPermission) { logger.error("[handle] can not produce message from queue [" + source.getName() + "] to queue [" + sink.getName() + "]"); event.getMessageContext().setThrowable(new RuntimeException("can not produce message from queue [" + source.getName() + "] to queue [" + sink.getName() + "]")); } } @Subscribe public void onRpcRequest(RpcRequestEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onRpcRequest =-=-=-"); MessageContext context = event.getMessageContext(); JsonRpcClient client = null; try { client = new JsonRpcClient(context.getChannel(), Constants.PROXY_EXCHANGE_NAME, context.getSink().getRoutingKey(), (int) context.getTimeout()); Object[] params = null; if (context.getOtherParams().get("params") != null) { params = (Object[]) context.getOtherParams().get("params"); } Object respObj = client.call(context.getOtherParams().get("methodName").toString(), params); context.getOtherParams().put("result", respObj); } catch (IOException e) { logger.error(e); event.getMessageContext().setThrowable(new RuntimeException(e)); } catch (TimeoutException e) { context.setIsTimeout(true); } catch (JsonRpcException e) { logger.error(e); event.getMessageContext().setThrowable(new RuntimeException(e)); } finally { try { if (client != null) client.close(); } catch (IOException e) { logger.error(e); } } } @Override public void process(MessageContext msgContext) { throw new UnsupportedOperationException("this method should be implemented in consume event processor!"); } public static class PermissionCheckEvent extends CarryEvent { } public static class RpcRequestEvent extends CarryEvent { } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/RpcResponseEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.eventbus.Subscribe; import com.messagebus.client.MessageContext; import com.messagebus.common.ExceptionHelper; import com.rabbitmq.tools.jsonrpc.JsonRpcServer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.concurrent.*; /** * Created by yanghua on 6/29/15. */ public class RpcResponseEventProcessor extends CommonEventProcessor { private static final Log logger = LogFactory.getLog(RpcResponseEventProcessor.class); public RpcResponseEventProcessor() { } @Subscribe public void onRpcResponse(RpcResponseEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onRpcResponse =-=-=-"); MessageContext context = event.getMessageContext(); ExecutorService executor = Executors.newFixedThreadPool(1); Future rpcServerLoopTask = executor.submit(new RpcServerLoopTask(context)); try { rpcServerLoopTask.get(context.getTimeout(), context.getTimeoutUnit()); } catch (InterruptedException e) { logger.info(" rpc server interrupted!"); } catch (ExecutionException e) { logger.error(e); event.getMessageContext().setThrowable(e); } catch (TimeoutException e) { logger.info("message loop timeout after : " + context.getTimeout() + " [" + context.getTimeoutUnit() + "]"); event.getMessageContext().setThrowable(e); } } @Override public void process(MessageContext msgContext) { throw new UnsupportedOperationException("this method should be implemented in consume event processor!"); } public static class RpcResponseEvent extends CarryEvent { } /** * inner class : rpc server loop task */ private class RpcServerLoopTask implements Runnable { private MessageContext msgContext; public RpcServerLoopTask(MessageContext msgContext) { this.msgContext = msgContext; } @Override public void run() { Class clazzOfInterface = (Class) msgContext.getOtherParams().get("clazzOfInterface"); Object obj = msgContext.getOtherParams().get("serviceProvider"); JsonRpcServer server = null; try { server = new JsonRpcServer(msgContext.getChannel(), msgContext.getSink().getQueueName(), clazzOfInterface, obj); server.mainloop(); } catch (IOException e) { ExceptionHelper.logException(logger, e, "real rpc responser"); throw new RuntimeException(e); } finally { if (server != null) { server.terminateMainloop(); } } } } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/carry/SubscribeEventProcessor.java ================================================ package com.messagebus.client.event.carry; import com.google.common.eventbus.Subscribe; import com.messagebus.client.ConfigManager; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.MessageContext; import com.messagebus.client.message.model.Message; import com.messagebus.client.model.MessageCarryType; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Created by yanghua on 6/27/15. */ public class SubscribeEventProcessor extends CommonEventProcessor { private static final Log logger = LogFactory.getLog(SubscribeEventProcessor.class); @Subscribe public void onValidate(ValidateEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onValidate =-=-=-"); super.onValidate(event); } @Subscribe public void onPermissionCheck(PermissionCheckEvent event) { super.exceptionCheck(event); logger.debug("=-=-=- event : onPermissionCheck =-=-=-"); MessageContext context = event.getMessageContext(); ConfigManager.Sink sink = context.getSink(); ConfigManager.Source source = context.getSource(); ConfigManager.Stream stream = context.getStream(); boolean hasPermission = MessageCarryType.lookup(sink.getType()).equals(MessageCarryType.SUBSCRIBE); hasPermission = hasPermission && (stream != null && stream.getSourceSecret().equals(source.getSecret()) && sink.getName().equals(stream.getSinkName())); if (!hasPermission) { logger.error("permission error : can not subscribe. may be communicate type is wrong . " + " current secret is : " + sink.getSecret()); event.getMessageContext().setThrowable(new RuntimeException("permission error : can not subscribe. may be communicate type is wrong . " + " current secret is : " + sink.getSecret())); } } @Override public void process(MessageContext msgContext) { Message msg = msgContext.getConsumeMsgs().get(0); IMessageReceiveListener receiveListener = msgContext.getReceiveListener(); receiveListener.onMessage(msg); } public static class ValidateEvent extends CarryEvent { } public static class PermissionCheckEvent extends CarryEvent { } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/component/ClientDestroyEvent.java ================================================ package com.messagebus.client.event.component; import com.messagebus.common.Event; /** * Created by yanghua on 6/25/15. */ public class ClientDestroyEvent extends Event { } ================================================ FILE: client/src/main/java/com/messagebus/client/event/component/ClientDestroyEventProcessor.java ================================================ package com.messagebus.client.event.component; import com.google.common.eventbus.Subscribe; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Created by yanghua on 6/25/15. */ public class ClientDestroyEventProcessor { private static final Log logger = LogFactory.getLog(ClientDestroyEventProcessor.class); @Subscribe public void onClientDestroy(ClientDestroyEvent event) { logger.debug("event : onClientDestroyEvent"); } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/component/ClientInitedEvent.java ================================================ package com.messagebus.client.event.component; import com.messagebus.common.Event; /** * Created by yanghua on 6/25/15. */ public class ClientInitedEvent extends Event { } ================================================ FILE: client/src/main/java/com/messagebus/client/event/component/ClientInitedEventProcessor.java ================================================ package com.messagebus.client.event.component; import com.google.common.eventbus.Subscribe; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Created by yanghua on 6/25/15. */ public class ClientInitedEventProcessor { private static final Log logger = LogFactory.getLog(ClientInitedEventProcessor.class); @Subscribe public void onClientInited(ClientInitedEvent event) { logger.debug("event : onClientInitedEvent"); } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/component/InnerEvent.java ================================================ package com.messagebus.client.event.component; import com.messagebus.client.message.model.Message; import com.messagebus.common.Event; /** * Created by yanghua on 10/28/15. */ public class InnerEvent extends Event { private Message msg; public InnerEvent() { } public Message getMsg() { return msg; } public void setMsg(Message msg) { this.msg = msg; } } ================================================ FILE: client/src/main/java/com/messagebus/client/event/component/NoticeEvent.java ================================================ package com.messagebus.client.event.component; import com.messagebus.client.message.model.Message; import com.messagebus.common.Event; /** * Created by yanghua on 6/29/15. */ public class NoticeEvent extends Event { private Message msg; public NoticeEvent() { } public Message getMsg() { return msg; } public void setMsg(Message notification) { this.msg = notification; } } ================================================ FILE: client/src/main/java/com/messagebus/client/extension/thrift/TAMQPClientTransport.java ================================================ package com.messagebus.client.extension.thrift; import com.messagebus.client.Messagebus; import com.messagebus.common.ExceptionHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; /** * Created by yanghua on 4/16/15. */ public class TAMQPClientTransport extends TTransport { private static final Log logger = LogFactory.getLog(TAMQPClientTransport.class); private Messagebus client; private String secret; private String target; private String token; private long timeout; private ByteArrayOutputStream reqMsgStream = new ByteArrayOutputStream(); private InputStream respMsgStream = null; public TAMQPClientTransport(Messagebus client, String secret, String target, String token, long timeoutOfMilliSecond) { this.client = client; this.secret = secret; this.target = target; this.token = token; this.timeout = timeoutOfMilliSecond; } @Override public boolean isOpen() { return client.isOpen(); } @Override public void open() throws TTransportException { } @Override public void close() { } @Override public int read(byte[] bytes, int i, int i1) throws TTransportException { if (this.respMsgStream == null) { throw new TTransportException("Response buffer is empty, no request."); } else { try { int iox = this.respMsgStream.read(bytes, i, i1); if (iox == -1) { throw new TTransportException("No more data available."); } else { return iox; } } catch (IOException var5) { throw new TTransportException(var5); } } } @Override public void write(byte[] bytes, int i, int i1) throws TTransportException { this.reqMsgStream.write(bytes, i, i1); } public void flush() throws TTransportException { byte[] data = this.reqMsgStream.toByteArray(); this.reqMsgStream.reset(); byte[] responseData = new byte[0]; try { responseData = this.client.primitiveRequest(secret, target, data, token, timeout); } catch (Exception e) { ExceptionHelper.logException(logger, e, "[flush]"); } this.respMsgStream = new ByteArrayInputStream(responseData); } } ================================================ FILE: client/src/main/java/com/messagebus/client/extension/thrift/ThriftMessageHandler.java ================================================ package com.messagebus.client.extension.thrift; import com.messagebus.client.IRpcMessageProcessor; import com.messagebus.common.ExceptionHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.thrift.TException; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.protocol.TProtocolFactory; import org.apache.thrift.transport.TIOStreamTransport; import org.apache.thrift.transport.TTransport; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; /** * Created by yanghua on 4/17/15. */ public class ThriftMessageHandler implements IRpcMessageProcessor { private static final Log logger = LogFactory.getLog(ThriftMessageHandler.class); private TProcessor processor; private TProtocolFactory inProtocolFactory; private TProtocolFactory outProtocolFactory; public ThriftMessageHandler(TProcessor processor, TProtocolFactory inProtocolFactory, TProtocolFactory outProtocolFactory) { this.processor = processor; this.inProtocolFactory = inProtocolFactory; this.outProtocolFactory = outProtocolFactory; } @Override public byte[] onRpcMessage(byte[] inMsg) { InputStream in = new ByteArrayInputStream(inMsg); OutputStream out = new ByteArrayOutputStream(); TTransport transport = new TIOStreamTransport(in, out); TProtocol inProtocol = inProtocolFactory.getProtocol(transport); TProtocol outProtocol = outProtocolFactory.getProtocol(transport); try { processor.process(inProtocol, outProtocol); return ((ByteArrayOutputStream) out).toByteArray(); } catch (TException e) { ExceptionHelper.logException(logger, e, "onRpcMessage"); throw new RuntimeException(e); } finally { transport.close(); } } } ================================================ FILE: client/src/main/java/com/messagebus/client/message/model/Message.java ================================================ package com.messagebus.client.message.model; import java.io.Serializable; import java.util.HashMap; import java.util.Map; public class Message implements Cloneable, Serializable { //header private long messageId; private String type; private long timestamp; private short priority; private String expiration; private Map headers; private String contentEncoding; private String contentType; private String replyTo; private String appId; private String userId; private String clusterId; private String correlationId; private short deliveryMode = 2; private byte[] content; public Message() { } public long getMessageId() { return messageId; } public void setMessageId(long messageId) { this.messageId = messageId; } public String getType() { return type; } public void setType(String type) { this.type = type; } public long getTimestamp() { return timestamp; } public void setTimestamp(long timestamp) { this.timestamp = timestamp; } public short getPriority() { return priority; } public void setPriority(short priority) { this.priority = priority; } public String getExpiration() { return expiration; } public short getDeliveryMode() { return 2; } public Map getHeaders() { return headers; } public void setHeaders(Map headers) { this.headers = headers; } public String getContentEncoding() { return contentEncoding; } public void setContentEncoding(String contentEncoding) { this.contentEncoding = contentEncoding; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } public String getReplyTo() { return replyTo; } public void setReplyTo(String replyTo) { this.replyTo = replyTo; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getUserId() { return userId; } protected void setUserId(String userId) { this.userId = null; } public String getClusterId() { return clusterId; } protected void setClusterId(String clusterId) { this.clusterId = null; } public String getCorrelationId() { return correlationId; } public void setCorrelationId(String correlationId) { this.correlationId = correlationId; } public byte[] getContent() { return content; } public void setContent(byte[] content) { this.content = content; } @Override public Object clone() throws CloneNotSupportedException { Message cloneObj = (Message) super.clone(); if (cloneObj.getHeaders() != null) { HashMap header = new HashMap(cloneObj.getHeaders().size()); for (Map.Entry item : cloneObj.getHeaders().entrySet()) { header.put(item.getKey(), item.getValue()); } cloneObj.setHeaders(header); } else { cloneObj.setHeaders(new HashMap(1)); } return cloneObj; } } ================================================ FILE: client/src/main/java/com/messagebus/client/message/model/MessageFactory.java ================================================ package com.messagebus.client.message.model; import com.messagebus.client.message.transfer.MessageHeaderTransfer; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.GetResponse; import com.rabbitmq.client.QueueingConsumer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class MessageFactory { private static final Log logger = LogFactory.getLog(MessageFactory.class); public static Message createMessage() { Message aMsg; aMsg = new Message(); return aMsg; } public static Message createMessage(QueueingConsumer.Delivery delivery) { AMQP.BasicProperties properties = delivery.getProperties(); byte[] msgBody = delivery.getBody(); Message msg = MessageFactory.createMessage(); initMessage(msg, properties, msgBody); return msg; } public static Message createMessage(GetResponse response) { AMQP.BasicProperties properties = response.getProps(); byte[] msgBody = response.getBody(); Message msg = MessageFactory.createMessage(); initMessage(msg, properties, msgBody); return msg; } private static void initMessage(Message msg, AMQP.BasicProperties properties, byte[] bodyData) { MessageHeaderTransfer.unbox(properties, msg); msg.setContent(bodyData); } } ================================================ FILE: client/src/main/java/com/messagebus/client/message/model/MessageJSONSerializer.java ================================================ package com.messagebus.client.message.model; import com.google.gson.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.Collection; public class MessageJSONSerializer { private static final Log logger = LogFactory.getLog(MessageJSONSerializer.class); private static final Gson gson = new GsonBuilder().serializeNulls().create(); public static String serialize(Message msg) { return gson.toJson(msg); } public static Message deSerialize(String msgStr) { JsonParser parser = new JsonParser(); JsonElement element = parser.parse(msgStr); Message msg = gson.fromJson(element, Message.class); return msg; } public static Message deSerialize(JsonElement msgElement) { JsonObject obj = msgElement.getAsJsonObject(); Message msg = gson.fromJson(obj.get("message"), Message.class); return msg; } public static String serializeMessages(Collection msgs) { return gson.toJson(msgs); } public static Message[] deSerializeMessages(String msgArrStr) { int i = 0; JsonParser parser = new JsonParser(); JsonElement element = parser.parse(msgArrStr); if (!element.isJsonArray()) { logger.error("unsupported original data. it should be a string of json object array "); throw new UnsupportedOperationException("unsupported original data. " + "it should be a string of json object array "); } Message[] msgs = new Message[element.getAsJsonArray().size()]; for (JsonElement e : element.getAsJsonArray()) { Message msg = gson.fromJson(e, Message.class); msgs[i++] = msg; } return msgs; } } ================================================ FILE: client/src/main/java/com/messagebus/client/message/transfer/MessageHeaderTransfer.java ================================================ package com.messagebus.client.message.transfer; import com.messagebus.client.message.model.Message; import com.rabbitmq.client.AMQP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.Date; public class MessageHeaderTransfer { private static final Log logger = LogFactory.getLog(MessageHeaderTransfer.class); public static AMQP.BasicProperties box(Message msg) { AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder(); return builder.messageId(String.valueOf(msg.getMessageId())) .appId(msg.getAppId()) .clusterId(msg.getClusterId()) .contentEncoding(msg.getContentEncoding()) .contentType(msg.getContentType()) .correlationId(msg.getCorrelationId()) .deliveryMode((int) msg.getDeliveryMode()) .expiration(msg.getExpiration()) .headers(msg.getHeaders()) .priority((int) msg.getPriority()) .replyTo(msg.getReplyTo()) .timestamp(new Date(msg.getTimestamp())) .type(msg.getType()) .userId(msg.getUserId()) .build(); } public static Message unbox(AMQP.BasicProperties properties, Message msg) { //common properties msg.setCorrelationId(properties.getCorrelationId()); msg.setHeaders(properties.getHeaders()); msg.setTimestamp(properties.getTimestamp().getTime()); String msgIdStr = properties.getMessageId(); if (msgIdStr != null && !msgIdStr.isEmpty()) msg.setMessageId(Long.parseLong(msgIdStr)); else logger.error("[unbox] illegal message id (can not be null) "); msg.setContentEncoding(properties.getContentEncoding()); msg.setContentType(properties.getContentType()); msg.setAppId(properties.getAppId()); msg.setReplyTo(properties.getReplyTo()); return msg; } } ================================================ FILE: client/src/main/java/com/messagebus/client/model/BaseModel.java ================================================ package com.messagebus.client.model; import java.io.Serializable; /** * the root class of all models */ public class BaseModel implements Serializable { @Override public String toString() { return "BaseModel"; } } ================================================ FILE: client/src/main/java/com/messagebus/client/model/Config.java ================================================ package com.messagebus.client.model; import java.io.Serializable; public class Config implements Serializable { private String key; private String value; public Config() { } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "Config{" + "key='" + key + '\'' + ", value='" + value + '\'' + '}'; } } ================================================ FILE: client/src/main/java/com/messagebus/client/model/HandlerModel.java ================================================ package com.messagebus.client.model; import java.io.Serializable; /** * the model of handler.xml's element */ public class HandlerModel extends BaseModel implements Serializable { private String handlerName; private String handlerPath; public HandlerModel() { } public String getHandlerName() { return handlerName; } public void setHandlerName(String handlerName) { this.handlerName = handlerName; } public String getHandlerPath() { return handlerPath; } public void setHandlerPath(String handlerPath) { this.handlerPath = handlerPath; } @Override public String toString() { return "Handler{" + "handlerName='" + handlerName + '\'' + ", handlerPath='" + handlerPath + '\'' + "} "; } } ================================================ FILE: client/src/main/java/com/messagebus/client/model/MessageCarryType.java ================================================ package com.messagebus.client.model; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** * message carry type */ public enum MessageCarryType { PRODUCE, CONSUME, REQUEST, RESPONSE, PUBLISH, SUBSCRIBE, BROADCAST, RPCREQUEST, RPCRESPONSE; private static final ConcurrentMap lookups = new ConcurrentHashMap(9); static { lookups.put("produce", PRODUCE); lookups.put("consume", CONSUME); lookups.put("request", REQUEST); lookups.put("response", RESPONSE); lookups.put("publish", PUBLISH); lookups.put("subscribe", SUBSCRIBE); lookups.put("broadcast", BROADCAST); lookups.put("rpcrequest", RPCREQUEST); lookups.put("rpcresponse", RPCRESPONSE); } public static MessageCarryType lookup(String strType) { MessageCarryType result = lookups.get(strType); if (result == null) throw new UnknownError("can not find the enum item of MessageCarryType which " + " maped the key : " + strType); return result; } public String stringOf() { for (Map.Entry entry : lookups.entrySet()) { if (entry.getValue().equals(this)) return entry.getKey(); } return ""; } } ================================================ FILE: client/src/main/resources/log4j.properties ================================================ #log4j.rootLogger=INFO, dailyLog #log4j.appender.dailyLog=org.apache.log4j.ConsoleAppender ##log4j.appender.dailyLog=org.apache.log4j.DailyRollingFileAppender ##log4j.appender.dailyLog.file=/Users/yanghua/Documents/ofbiz/mini-ofbiz/runtime/logs/access_log ##log4j.appender.dailyLog.DatePattern='.'yyyy-MM-dd ##log4j.appender.dailyLog.encoding=utf-8 #log4j.appender.dailyLog.layout=org.apache.log4j.PatternLayout #log4j.appender.dailyLog.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss,SSS}] [%-5p] [messagebus] [messagebus] [%t] [%c] [%F:%L] %m %n ================================================ FILE: client/src/test/java/com/messagebus/client/api/Broadcast.java ================================================ package com.messagebus.client.api; import com.google.common.eventbus.Subscribe; import com.messagebus.client.core.BaseTestCase; import com.messagebus.client.event.component.NoticeEvent; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 3/20/15. */ public class Broadcast extends BaseTestCase { private static final Log logger = LogFactory.getLog(Broadcast.class); @Override public void setUp() throws Exception { super.setUp(); } @Override public void tearDown() throws Exception { super.tearDown(); } public void testBroadcast() throws Exception { String secret = "kljasdoifqoikjhhhqwhebasdfasdf"; Message msg = MessageFactory.createMessage(); msg.setContentType("text/plain"); msg.setContentEncoding("utf-8"); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); NotificationEventProcessor eventProcessor = new NotificationEventProcessor(); client.registerEventProcessor(eventProcessor); client.broadcast(secret, new Message[]{msg}); TimeUnit.SECONDS.sleep(5); client.unregisterEventProcessor(eventProcessor); } public static class NotificationEventProcessor { @Subscribe public void onNotification(NoticeEvent event) { logger.info("onNotification"); Message message = event.getMsg(); assertNotNull(message); assertEquals("test", new String(message.getContent(), Constants.CHARSET_OF_UTF8)); } } } ================================================ FILE: client/src/test/java/com/messagebus/client/api/ComponentEventListener.java ================================================ package com.messagebus.client.api; import com.google.common.eventbus.Subscribe; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.core.BaseTestCase; import com.messagebus.client.event.component.ClientDestroyEvent; import com.messagebus.client.event.component.ClientInitedEvent; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Test; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 6/25/15. */ public class ComponentEventListener extends BaseTestCase { private static final Log logger = LogFactory.getLog(ComponentEventListener.class); @Override public void setUp() throws Exception { } @Override public void tearDown() throws Exception { } @Test public void testComponentEvent() throws Exception { singlePool = new MessagebusSinglePool(zkConnectionStr); singlePool.registerComponentEventListener(new EventListener()); client = singlePool.getResource(); TimeUnit.SECONDS.sleep(5); singlePool.returnResource(client); singlePool.destroy(); } public class EventListener { @Subscribe public void onClientInited(ClientInitedEvent event) { logger.debug(" outer of component event : onClientInited"); } @Subscribe public void onClientDestroy(ClientDestroyEvent event) { logger.debug(" outer of component event : onClientDestroy"); } } } ================================================ FILE: client/src/test/java/com/messagebus/client/api/ProduceConsume.java ================================================ package com.messagebus.client.api; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.core.BaseTestCase; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.List; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 3/20/15. */ public class ProduceConsume extends BaseTestCase { private static final Log logger = LogFactory.getLog(ProduceConsume.class); private String consumeSecret = "kjhasdfhlkuqjhgaebjhasgdfabfak"; @Override public void setUp() throws Exception { super.setUp(); } @Override public void tearDown() throws Exception { super.tearDown(); } private void commonProduce() { String secret = "kljasdoifqoikjhhhqwhebasdfasdf"; String token = "hlkasjdhfkqlwhlfalksjdhgssssas"; Message msg = MessageFactory.createMessage(); msg.setContentType("text/plain"); msg.setContentEncoding("utf-8"); msg.setContent("test".getBytes()); client.produce(secret, "erpDemoConsume", msg, token); } public void testSimpleProduceConsume() throws Exception { commonProduce(); List msgs = client.consume(consumeSecret, 1); assertNotNull(msgs); assertEquals(1, msgs.size()); Message result = msgs.get(0); assertNotNull(result); assertNotNull(result.getContent()); assertEquals("test", new String(result.getContent())); for (Message item : msgs) { logger.info(item.getMessageId()); } } public void testProduceAndConsumeWithPushStyle() { commonProduce(); client.consume(consumeSecret, 3, TimeUnit.SECONDS, new IMessageReceiveListener() { @Override public void onMessage(Message message) { assertNotNull(message); assertEquals("test", new String(message.getContent(), Constants.CHARSET_OF_UTF8)); } }); } } ================================================ FILE: client/src/test/java/com/messagebus/client/api/ProduceConsumeLoopback.java ================================================ //package com.messagebus.client.api; // //import com.messagebus.client.IMessageReceiveListener; //import com.messagebus.client.core.BaseTestCase; //import com.messagebus.client.message.model.Message; //import com.messagebus.client.message.model.MessageFactory; //import com.messagebus.client.message.model.MessageType; //import com.messagebus.common.Constants; // //import java.util.concurrent.TimeUnit; // ///** // * Created by yanghua on 3/25/15. // */ //public class ProduceConsumeLoopback extends BaseTestCase { // // @Override // public void setUp() throws Exception { // super.setUp(); // } // // @Override // public void tearDown() throws Exception { // super.tearDown(); // } // // private void commonProduce() { // String secret = "jnmzqwemnjaksdfqjnkajfjasndfnw"; // String token = "jnmzqwemnjaksdfqjnkajfjasndfnw"; // // Message msg = MessageFactory.createMessage(MessageType.QueueMessage); // msg.setContentType("text/plain"); // msg.setContentEncoding("utf-8"); // msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); // // client.produce(secret, "erpDemoProduce-Consume", msg, token); // } // // public void testLoopBack() throws Exception { // commonProduce(); // // String consumeSecret = "jnmzqwemnjaksdfqjnkajfjasndfnw"; // client.consume(consumeSecret, 10, TimeUnit.SECONDS, new IMessageReceiveListener() { // @Override // public void onMessage(Message message) { // assertNotNull(message); // assertEquals("test", new String(message.getContent(), Constants.CHARSET_OF_UTF8)); // } // }); // } //} ================================================ FILE: client/src/test/java/com/messagebus/client/api/PublishSubscribe.java ================================================ package com.messagebus.client.api; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.core.BaseTestCase; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 3/20/15. */ public class PublishSubscribe extends BaseTestCase { private static final Log logger = LogFactory.getLog(PublishSubscribe.class); @Override public void setUp() throws Exception { super.setUp(); } @Override public void tearDown() throws Exception { super.tearDown(); } public void testPublishAndSubscribe() throws Exception { String secret = "oiqwenncuicnsdfuasdfnkajkwqowe"; Message msg = MessageFactory.createMessage(); msg.setContentType("text/plain"); msg.setContentEncoding("utf-8"); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); client.publish(secret, new Message[]{msg}); //--------------------------subscribe------------------------------- secret = "nckljsenlkjanefluiwnlanfmsdfas"; String token = "nclajsdljhqiuwehfiusaiudfhiausd"; client.subscribe(secret, "erpDemoPublish", token, new IMessageReceiveListener() { @Override public void onMessage(Message message) { assertNotNull(message); assertEquals("test", new String(message.getContent(), Constants.CHARSET_OF_UTF8)); } }, 3, TimeUnit.SECONDS); } } ================================================ FILE: client/src/test/java/com/messagebus/client/api/RequestResponse.java ================================================ package com.messagebus.client.api; import com.google.common.base.Strings; import com.messagebus.client.IRequestListener; import com.messagebus.client.MessageResponseTimeoutException; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.core.BaseTestCase; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 3/20/15. */ public class RequestResponse extends BaseTestCase { private static final Log logger = LogFactory.getLog(RequestResponse.class); @Override public void setUp() throws Exception { } @Override public void tearDown() throws Exception { } public void testRequestAndResponse() throws Exception { new Thread(new Runnable() { @Override public void run() { final String secret = "muciasnajjkasbdfbaskjdfkjkasja"; MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); final Messagebus client = singlePool.getResource(); client.response(secret, new IRequestListener() { @Override public Message onRequest(Message requestMsg) { if (Strings.isNullOrEmpty(requestMsg.getCorrelationId())) { logger.info("got requested message : " + requestMsg.getCorrelationId()); } assertNotNull(requestMsg); assertEquals("test", new String(requestMsg.getContent(), Constants.CHARSET_OF_UTF8)); Message respMsg = MessageFactory.createMessage(); respMsg.setContentType("text/plain"); respMsg.setContentEncoding("utf-8"); respMsg.setCorrelationId(requestMsg.getCorrelationId()); respMsg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); return respMsg; } }, 15, TimeUnit.SECONDS); singlePool.returnResource(client); singlePool.destroy(); } }).start(); String secret = "iuoqiwejicaoisfaisfbsqewnfjnfa"; String token = "cakjdhfjasdflqjoiajsdjflqkuwef"; MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); Messagebus client = singlePool.getResource(); Message msg = MessageFactory.createMessage(); msg.setContentType("text/plain"); msg.setContentEncoding("utf-8"); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); Message responseMsg = null; try { responseMsg = client.request(secret, "erpDemoResponse", msg, token, 10); } catch (MessageResponseTimeoutException e) { e.printStackTrace(); } singlePool.returnResource(client); singlePool.destroy(); assertNotNull(responseMsg); assertEquals("test", new String(responseMsg.getContent(), Constants.CHARSET_OF_UTF8)); } } ================================================ FILE: client/src/test/java/com/messagebus/client/api/RpcRequestResponse.java ================================================ package com.messagebus.client.api; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.core.BaseTestCase; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 4/8/15. */ public class RpcRequestResponse extends BaseTestCase { private static final Log logger = LogFactory.getLog(RpcRequestResponse.class); @Override public void setUp() throws Exception { super.setUp(); } @Override public void tearDown() throws Exception { super.tearDown(); } public void testSimpleRpc() throws Exception { new Thread(new Runnable() { @Override public void run() { String secret = "jhliquwdlaisudfqbjhasdfulquias"; MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); final Messagebus client = singlePool.getResource(); client.callback(secret, TestInterface.class, new HelloServiceProvider(), 10, TimeUnit.SECONDS); singlePool.returnResource(client); singlePool.destroy(); } }).start(); MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); Messagebus client = singlePool.getResource(); String secret = "kliwhiduhaiucvarkjajksdbfkjabw"; String targetQueue = "erpDemoRpcResponse"; String token = "klasehnfkljashdnflhkjahwlekdjf"; String methodName = "sayHello"; Object responseObj = client.call(secret, targetQueue, methodName, new Object[0], token, 10000); assertNull(responseObj); singlePool.returnResource(client); singlePool.destroy(); } public void testReturnValueRpc() throws Exception { new Thread(new Runnable() { @Override public void run() { String secret = "jhliquwdlaisudfqbjhasdfulquias"; MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); final Messagebus client = singlePool.getResource(); client.callback(secret, TestInterface.class, new HelloServiceProvider(), 10, TimeUnit.SECONDS); singlePool.returnResource(client); singlePool.destroy(); } }).start(); MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); Messagebus client = singlePool.getResource(); String secret = "kliwhiduhaiucvarkjajksdbfkjabw"; String targetQueue = "erpDemoRpcResponse"; String token = "klasehnfkljashdnflhkjahwlekdjf"; String methodName = "returnValueMethod"; Object responseObj = client.call(secret, targetQueue, methodName, new Object[0], token, 10000); assertNotNull(responseObj); assertEquals("hello world", responseObj.toString()); singlePool.returnResource(client); singlePool.destroy(); } public void testParamNoReturnValueRpc() throws Exception { new Thread(new Runnable() { @Override public void run() { String secret = "jhliquwdlaisudfqbjhasdfulquias"; MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); final Messagebus client = singlePool.getResource(); client.callback(secret, TestInterface.class, new HelloServiceProvider(), 10, TimeUnit.SECONDS); singlePool.returnResource(client); singlePool.destroy(); } }).start(); MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); Messagebus client = singlePool.getResource(); String secret = "kliwhiduhaiucvarkjajksdbfkjabw"; String targetQueue = "erpDemoRpcResponse"; String token = "klasehnfkljashdnflhkjahwlekdjf"; String methodName = "printParam"; Object responseObj = client.call(secret, targetQueue, methodName, new Object[]{"hello world"}, token, 10000); singlePool.returnResource(client); singlePool.destroy(); } public void testReturnParamValueRpc() throws Exception { new Thread(new Runnable() { @Override public void run() { String secret = "jhliquwdlaisudfqbjhasdfulquias"; MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); final Messagebus client = singlePool.getResource(); client.callback(secret, TestInterface.class, new HelloServiceProvider(), 10, TimeUnit.SECONDS); singlePool.returnResource(client); singlePool.destroy(); } }).start(); MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); Messagebus client = singlePool.getResource(); String secret = "kliwhiduhaiucvarkjajksdbfkjabw"; String targetQueue = "erpDemoRpcResponse"; String token = "klasehnfkljashdnflhkjahwlekdjf"; String methodName = "returnParam"; Object responseObj = client.call(secret, targetQueue, methodName, new Object[]{"hello world"}, token, 10000); assertNotNull(responseObj); assertEquals("hello world", responseObj.toString()); singlePool.returnResource(client); singlePool.destroy(); } public static interface TestInterface { public void sayHello(); public String returnValueMethod(); public void printParam(String arg1); public String returnParam(String arg1); } public static class HelloServiceProvider implements TestInterface { @Override public void sayHello() { logger.info("hello..."); } @Override public String returnValueMethod() { return "hello world"; } @Override public void printParam(String arg1) { System.out.println(arg1); } @Override public String returnParam(String arg1) { return arg1; } } } ================================================ FILE: client/src/test/java/com/messagebus/client/core/BaseTestCase.java ================================================ package com.messagebus.client.core; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.common.TestVariableInfo; import junit.framework.TestCase; /** * Created by yanghua on 3/20/15. */ public class BaseTestCase extends TestCase { protected String zkConnectionStr = TestVariableInfo.ZK_CONNECTION_STRING; protected MessagebusSinglePool singlePool; protected Messagebus client; @Override public void setUp() throws Exception { singlePool = new MessagebusSinglePool(zkConnectionStr); client = singlePool.getResource(); } @Override public void tearDown() throws Exception { singlePool.returnResource(client); singlePool.destroy(); } } ================================================ FILE: client/src/test/java/com/messagebus/client/core/MessageUtil.java ================================================ package com.messagebus.client.core; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; /** * Created by yanghua on 3/27/15. */ public class MessageUtil { public static Message create(int sizeOfByte) { Message msg = MessageFactory.createMessage(); byte[] content = generate(sizeOfByte); msg.setContent(content); return msg; } private static byte[] generate(int sizeOfByte) { byte[] result = new byte[sizeOfByte]; for (int i = 0; i < sizeOfByte; i++) { result[i] = (byte) 1; } return result; } } ================================================ FILE: client/src/test/java/com/messagebus/client/feature/CalcService.java ================================================ /** * Autogenerated by Thrift Compiler (0.9.2) *

* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING * * @generated */ package com.messagebus.client.feature; import org.apache.thrift.EncodingUtils; import org.apache.thrift.TException; import org.apache.thrift.async.AsyncMethodCallback; import org.apache.thrift.protocol.TTupleProtocol; import org.apache.thrift.scheme.IScheme; import org.apache.thrift.scheme.SchemeFactory; import org.apache.thrift.scheme.StandardScheme; import org.apache.thrift.scheme.TupleScheme; import org.apache.thrift.server.AbstractNonblockingServer.AsyncFrameBuffer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.Generated; import java.util.*; @SuppressWarnings({"cast", "rawtypes", "serial", "unchecked"}) @Generated(value = "Autogenerated by Thrift Compiler (0.9.2)", date = "2015-4-17") public class CalcService { public interface Iface { public int calcSum() throws TException; } public interface AsyncIface { public void calcSum(AsyncMethodCallback resultHandler) throws TException; } public static class Client extends org.apache.thrift.TServiceClient implements Iface { public static class Factory implements org.apache.thrift.TServiceClientFactory { public Factory() { } public Client getClient(org.apache.thrift.protocol.TProtocol prot) { return new Client(prot); } public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) { return new Client(iprot, oprot); } } public Client(org.apache.thrift.protocol.TProtocol prot) { super(prot, prot); } public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) { super(iprot, oprot); } public int calcSum() throws TException { send_calcSum(); return recv_calcSum(); } public void send_calcSum() throws TException { calcSum_args args = new calcSum_args(); sendBase("calcSum", args); } public int recv_calcSum() throws TException { calcSum_result result = new calcSum_result(); receiveBase(result, "calcSum"); if (result.isSetSuccess()) { return result.success; } throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, "calcSum failed: unknown result"); } } public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface { public static class Factory implements org.apache.thrift.async.TAsyncClientFactory { private org.apache.thrift.async.TAsyncClientManager clientManager; private org.apache.thrift.protocol.TProtocolFactory protocolFactory; public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) { this.clientManager = clientManager; this.protocolFactory = protocolFactory; } public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) { return new AsyncClient(protocolFactory, clientManager, transport); } } public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) { super(protocolFactory, clientManager, transport); } public void calcSum(AsyncMethodCallback resultHandler) throws TException { checkReady(); calcSum_call method_call = new calcSum_call(resultHandler, this, ___protocolFactory, ___transport); this.___currentMethod = method_call; ___manager.call(method_call); } public static class calcSum_call extends org.apache.thrift.async.TAsyncMethodCall { public calcSum_call(AsyncMethodCallback resultHandler, org.apache.thrift.async.TAsyncClient client, org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.transport.TNonblockingTransport transport) throws TException { super(client, protocolFactory, transport, resultHandler, false); } public void write_args(org.apache.thrift.protocol.TProtocol prot) throws TException { prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage("calcSum", org.apache.thrift.protocol.TMessageType.CALL, 0)); calcSum_args args = new calcSum_args(); args.write(prot); prot.writeMessageEnd(); } public int getResult() throws TException { if (getState() != State.RESPONSE_READ) { throw new IllegalStateException("Method call not finished!"); } org.apache.thrift.transport.TMemoryInputTransport memoryTransport = new org.apache.thrift.transport.TMemoryInputTransport(getFrameBuffer().array()); org.apache.thrift.protocol.TProtocol prot = client.getProtocolFactory().getProtocol(memoryTransport); return (new Client(prot)).recv_calcSum(); } } } public static class Processor extends org.apache.thrift.TBaseProcessor implements org.apache.thrift.TProcessor { private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName()); public Processor(I iface) { super(iface, getProcessMap(new HashMap>())); } protected Processor(I iface, Map> processMap) { super(iface, getProcessMap(processMap)); } private static Map> getProcessMap(Map> processMap) { processMap.put("calcSum", new calcSum()); return processMap; } public static class calcSum extends org.apache.thrift.ProcessFunction { public calcSum() { super("calcSum"); } public calcSum_args getEmptyArgsInstance() { return new calcSum_args(); } protected boolean isOneway() { return false; } public calcSum_result getResult(I iface, calcSum_args args) throws TException { calcSum_result result = new calcSum_result(); result.success = iface.calcSum(); result.setSuccessIsSet(true); return result; } } } public static class AsyncProcessor extends org.apache.thrift.TBaseAsyncProcessor { private static final Logger LOGGER = LoggerFactory.getLogger(AsyncProcessor.class.getName()); public AsyncProcessor(I iface) { super(iface, getProcessMap(new HashMap>())); } protected AsyncProcessor(I iface, Map> processMap) { super(iface, getProcessMap(processMap)); } private static Map> getProcessMap(Map> processMap) { processMap.put("calcSum", new calcSum()); return processMap; } public static class calcSum extends org.apache.thrift.AsyncProcessFunction { public calcSum() { super("calcSum"); } public calcSum_args getEmptyArgsInstance() { return new calcSum_args(); } public AsyncMethodCallback getResultHandler(final AsyncFrameBuffer fb, final int seqid) { final org.apache.thrift.AsyncProcessFunction fcall = this; return new AsyncMethodCallback() { public void onComplete(Integer o) { calcSum_result result = new calcSum_result(); result.success = o; result.setSuccessIsSet(true); try { fcall.sendResponse(fb, result, org.apache.thrift.protocol.TMessageType.REPLY, seqid); return; } catch (Exception e) { LOGGER.error("Exception writing to internal frame buffer", e); } fb.close(); } public void onError(Exception e) { byte msgType = org.apache.thrift.protocol.TMessageType.REPLY; org.apache.thrift.TBase msg; calcSum_result result = new calcSum_result(); { msgType = org.apache.thrift.protocol.TMessageType.EXCEPTION; msg = (org.apache.thrift.TBase) new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.INTERNAL_ERROR, e.getMessage()); } try { fcall.sendResponse(fb, msg, msgType, seqid); return; } catch (Exception ex) { LOGGER.error("Exception writing to internal frame buffer", ex); } fb.close(); } }; } protected boolean isOneway() { return false; } public void start(I iface, calcSum_args args, AsyncMethodCallback resultHandler) throws TException { iface.calcSum(resultHandler); } } } public static class calcSum_args implements org.apache.thrift.TBase, java.io.Serializable, Cloneable, Comparable { private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("calcSum_args"); private static final Map, SchemeFactory> schemes = new HashMap, SchemeFactory>(); static { schemes.put(StandardScheme.class, new calcSum_argsStandardSchemeFactory()); schemes.put(TupleScheme.class, new calcSum_argsTupleSchemeFactory()); } /** * The set of fields this struct contains, along with convenience methods for finding and manipulating them. */ public enum _Fields implements org.apache.thrift.TFieldIdEnum { ; private static final Map byName = new HashMap(); static { for (_Fields field : EnumSet.allOf(_Fields.class)) { byName.put(field.getFieldName(), field); } } /** * Find the _Fields constant that matches fieldId, or null if its not found. */ public static _Fields findByThriftId(int fieldId) { switch (fieldId) { default: return null; } } /** * Find the _Fields constant that matches fieldId, throwing an exception * if it is not found. */ public static _Fields findByThriftIdOrThrow(int fieldId) { _Fields fields = findByThriftId(fieldId); if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!"); return fields; } /** * Find the _Fields constant that matches name, or null if its not found. */ public static _Fields findByName(String name) { return byName.get(name); } private final short _thriftId; private final String _fieldName; _Fields(short thriftId, String fieldName) { _thriftId = thriftId; _fieldName = fieldName; } public short getThriftFieldId() { return _thriftId; } public String getFieldName() { return _fieldName; } } public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap; static { Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class); metaDataMap = Collections.unmodifiableMap(tmpMap); org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(calcSum_args.class, metaDataMap); } public calcSum_args() { } /** * Performs a deep copy on other. */ public calcSum_args(calcSum_args other) { } public calcSum_args deepCopy() { return new calcSum_args(this); } @Override public void clear() { } public void setFieldValue(_Fields field, Object value) { switch (field) { } } public Object getFieldValue(_Fields field) { switch (field) { } throw new IllegalStateException(); } /** * Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */ public boolean isSet(_Fields field) { if (field == null) { throw new IllegalArgumentException(); } switch (field) { } throw new IllegalStateException(); } @Override public boolean equals(Object that) { if (that == null) return false; if (that instanceof calcSum_args) return this.equals((calcSum_args) that); return false; } public boolean equals(calcSum_args that) { if (that == null) return false; return true; } @Override public int hashCode() { List list = new ArrayList(); return list.hashCode(); } @Override public int compareTo(calcSum_args other) { if (!getClass().equals(other.getClass())) { return getClass().getName().compareTo(other.getClass().getName()); } int lastComparison = 0; return 0; } public _Fields fieldForId(int fieldId) { return _Fields.findByThriftId(fieldId); } public void read(org.apache.thrift.protocol.TProtocol iprot) throws TException { schemes.get(iprot.getScheme()).getScheme().read(iprot, this); } public void write(org.apache.thrift.protocol.TProtocol oprot) throws TException { schemes.get(oprot.getScheme()).getScheme().write(oprot, this); } @Override public String toString() { StringBuilder sb = new StringBuilder("calcSum_args("); boolean first = true; sb.append(")"); return sb.toString(); } public void validate() throws TException { // check for required fields // check for sub-struct validity } private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { try { write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out))); } catch (TException te) { throw new java.io.IOException(te); } } private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { try { read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in))); } catch (TException te) { throw new java.io.IOException(te); } } private static class calcSum_argsStandardSchemeFactory implements SchemeFactory { public calcSum_argsStandardScheme getScheme() { return new calcSum_argsStandardScheme(); } } private static class calcSum_argsStandardScheme extends StandardScheme { public void read(org.apache.thrift.protocol.TProtocol iprot, calcSum_args struct) throws TException { org.apache.thrift.protocol.TField schemeField; iprot.readStructBegin(); while (true) { schemeField = iprot.readFieldBegin(); if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { break; } switch (schemeField.id) { default: org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } iprot.readFieldEnd(); } iprot.readStructEnd(); // check for required fields of primitive type, which can't be checked in the validate method struct.validate(); } public void write(org.apache.thrift.protocol.TProtocol oprot, calcSum_args struct) throws TException { struct.validate(); oprot.writeStructBegin(STRUCT_DESC); oprot.writeFieldStop(); oprot.writeStructEnd(); } } private static class calcSum_argsTupleSchemeFactory implements SchemeFactory { public calcSum_argsTupleScheme getScheme() { return new calcSum_argsTupleScheme(); } } private static class calcSum_argsTupleScheme extends TupleScheme { @Override public void write(org.apache.thrift.protocol.TProtocol prot, calcSum_args struct) throws TException { TTupleProtocol oprot = (TTupleProtocol) prot; } @Override public void read(org.apache.thrift.protocol.TProtocol prot, calcSum_args struct) throws TException { TTupleProtocol iprot = (TTupleProtocol) prot; } } } public static class calcSum_result implements org.apache.thrift.TBase, java.io.Serializable, Cloneable, Comparable { private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("calcSum_result"); private static final org.apache.thrift.protocol.TField SUCCESS_FIELD_DESC = new org.apache.thrift.protocol.TField("success", org.apache.thrift.protocol.TType.I32, (short) 0); private static final Map, SchemeFactory> schemes = new HashMap, SchemeFactory>(); static { schemes.put(StandardScheme.class, new calcSum_resultStandardSchemeFactory()); schemes.put(TupleScheme.class, new calcSum_resultTupleSchemeFactory()); } public int success; // required /** * The set of fields this struct contains, along with convenience methods for finding and manipulating them. */ public enum _Fields implements org.apache.thrift.TFieldIdEnum { SUCCESS((short) 0, "success"); private static final Map byName = new HashMap(); static { for (_Fields field : EnumSet.allOf(_Fields.class)) { byName.put(field.getFieldName(), field); } } /** * Find the _Fields constant that matches fieldId, or null if its not found. */ public static _Fields findByThriftId(int fieldId) { switch (fieldId) { case 0: // SUCCESS return SUCCESS; default: return null; } } /** * Find the _Fields constant that matches fieldId, throwing an exception * if it is not found. */ public static _Fields findByThriftIdOrThrow(int fieldId) { _Fields fields = findByThriftId(fieldId); if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!"); return fields; } /** * Find the _Fields constant that matches name, or null if its not found. */ public static _Fields findByName(String name) { return byName.get(name); } private final short _thriftId; private final String _fieldName; _Fields(short thriftId, String fieldName) { _thriftId = thriftId; _fieldName = fieldName; } public short getThriftFieldId() { return _thriftId; } public String getFieldName() { return _fieldName; } } // isset id assignments private static final int __SUCCESS_ISSET_ID = 0; private byte __isset_bitfield = 0; public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap; static { Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class); tmpMap.put(_Fields.SUCCESS, new org.apache.thrift.meta_data.FieldMetaData("success", org.apache.thrift.TFieldRequirementType.DEFAULT, new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.I32))); metaDataMap = Collections.unmodifiableMap(tmpMap); org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(calcSum_result.class, metaDataMap); } public calcSum_result() { } public calcSum_result( int success) { this(); this.success = success; setSuccessIsSet(true); } /** * Performs a deep copy on other. */ public calcSum_result(calcSum_result other) { __isset_bitfield = other.__isset_bitfield; this.success = other.success; } public calcSum_result deepCopy() { return new calcSum_result(this); } @Override public void clear() { setSuccessIsSet(false); this.success = 0; } public int getSuccess() { return this.success; } public calcSum_result setSuccess(int success) { this.success = success; setSuccessIsSet(true); return this; } public void unsetSuccess() { __isset_bitfield = EncodingUtils.clearBit(__isset_bitfield, __SUCCESS_ISSET_ID); } /** * Returns true if field success is set (has been assigned a value) and false otherwise */ public boolean isSetSuccess() { return EncodingUtils.testBit(__isset_bitfield, __SUCCESS_ISSET_ID); } public void setSuccessIsSet(boolean value) { __isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __SUCCESS_ISSET_ID, value); } public void setFieldValue(_Fields field, Object value) { switch (field) { case SUCCESS: if (value == null) { unsetSuccess(); } else { setSuccess((Integer) value); } break; } } public Object getFieldValue(_Fields field) { switch (field) { case SUCCESS: return Integer.valueOf(getSuccess()); } throw new IllegalStateException(); } /** * Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */ public boolean isSet(_Fields field) { if (field == null) { throw new IllegalArgumentException(); } switch (field) { case SUCCESS: return isSetSuccess(); } throw new IllegalStateException(); } @Override public boolean equals(Object that) { if (that == null) return false; if (that instanceof calcSum_result) return this.equals((calcSum_result) that); return false; } public boolean equals(calcSum_result that) { if (that == null) return false; boolean this_present_success = true; boolean that_present_success = true; if (this_present_success || that_present_success) { if (!(this_present_success && that_present_success)) return false; if (this.success != that.success) return false; } return true; } @Override public int hashCode() { List list = new ArrayList(); boolean present_success = true; list.add(present_success); if (present_success) list.add(success); return list.hashCode(); } @Override public int compareTo(calcSum_result other) { if (!getClass().equals(other.getClass())) { return getClass().getName().compareTo(other.getClass().getName()); } int lastComparison = 0; lastComparison = Boolean.valueOf(isSetSuccess()).compareTo(other.isSetSuccess()); if (lastComparison != 0) { return lastComparison; } if (isSetSuccess()) { lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.success, other.success); if (lastComparison != 0) { return lastComparison; } } return 0; } public _Fields fieldForId(int fieldId) { return _Fields.findByThriftId(fieldId); } public void read(org.apache.thrift.protocol.TProtocol iprot) throws TException { schemes.get(iprot.getScheme()).getScheme().read(iprot, this); } public void write(org.apache.thrift.protocol.TProtocol oprot) throws TException { schemes.get(oprot.getScheme()).getScheme().write(oprot, this); } @Override public String toString() { StringBuilder sb = new StringBuilder("calcSum_result("); boolean first = true; sb.append("success:"); sb.append(this.success); first = false; sb.append(")"); return sb.toString(); } public void validate() throws TException { // check for required fields // check for sub-struct validity } private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { try { write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out))); } catch (TException te) { throw new java.io.IOException(te); } } private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { try { // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor. __isset_bitfield = 0; read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in))); } catch (TException te) { throw new java.io.IOException(te); } } private static class calcSum_resultStandardSchemeFactory implements SchemeFactory { public calcSum_resultStandardScheme getScheme() { return new calcSum_resultStandardScheme(); } } private static class calcSum_resultStandardScheme extends StandardScheme { public void read(org.apache.thrift.protocol.TProtocol iprot, calcSum_result struct) throws TException { org.apache.thrift.protocol.TField schemeField; iprot.readStructBegin(); while (true) { schemeField = iprot.readFieldBegin(); if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { break; } switch (schemeField.id) { case 0: // SUCCESS if (schemeField.type == org.apache.thrift.protocol.TType.I32) { struct.success = iprot.readI32(); struct.setSuccessIsSet(true); } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } break; default: org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } iprot.readFieldEnd(); } iprot.readStructEnd(); // check for required fields of primitive type, which can't be checked in the validate method struct.validate(); } public void write(org.apache.thrift.protocol.TProtocol oprot, calcSum_result struct) throws TException { struct.validate(); oprot.writeStructBegin(STRUCT_DESC); if (struct.isSetSuccess()) { oprot.writeFieldBegin(SUCCESS_FIELD_DESC); oprot.writeI32(struct.success); oprot.writeFieldEnd(); } oprot.writeFieldStop(); oprot.writeStructEnd(); } } private static class calcSum_resultTupleSchemeFactory implements SchemeFactory { public calcSum_resultTupleScheme getScheme() { return new calcSum_resultTupleScheme(); } } private static class calcSum_resultTupleScheme extends TupleScheme { @Override public void write(org.apache.thrift.protocol.TProtocol prot, calcSum_result struct) throws TException { TTupleProtocol oprot = (TTupleProtocol) prot; BitSet optionals = new BitSet(); if (struct.isSetSuccess()) { optionals.set(0); } oprot.writeBitSet(optionals, 1); if (struct.isSetSuccess()) { oprot.writeI32(struct.success); } } @Override public void read(org.apache.thrift.protocol.TProtocol prot, calcSum_result struct) throws TException { TTupleProtocol iprot = (TTupleProtocol) prot; BitSet incoming = iprot.readBitSet(1); if (incoming.get(0)) { struct.success = iprot.readI32(); struct.setSuccessIsSet(true); } } } } } ================================================ FILE: client/src/test/java/com/messagebus/client/feature/CalcServiceImpl.java ================================================ package com.messagebus.client.feature; import org.apache.thrift.TException; /** * Created by yanghua on 4/17/15. */ public class CalcServiceImpl implements CalcService.Iface { @Override public int calcSum() throws TException { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } return sum; } } ================================================ FILE: client/src/test/java/com/messagebus/client/feature/MessageBodySize.java ================================================ package com.messagebus.client.feature; import com.messagebus.client.core.BaseTestCase; import com.messagebus.client.core.MessageUtil; import com.messagebus.client.message.model.Message; /** * Created by yanghua on 3/27/15. */ public class MessageBodySize extends BaseTestCase { @Override public void setUp() throws Exception { super.setUp(); } @Override public void tearDown() throws Exception { super.tearDown(); } public void testMsgBodySizeOverhead() throws Exception { String secret = "kljasdoifqoikjhhhqwhebasdfasdf"; String token = "hlkasjdhfkqlwhlfalksjdhgssssas"; Message msg = MessageUtil.create(4000); client.produce(secret, "emapDemoConsume", msg, token); } } ================================================ FILE: client/src/test/java/com/messagebus/client/feature/MessageNumLimit.java ================================================ package com.messagebus.client.feature; import com.messagebus.client.core.BaseTestCase; import com.messagebus.client.core.MessageUtil; import com.messagebus.client.message.model.Message; /** * Created by yanghua on 3/27/15. */ public class MessageNumLimit extends BaseTestCase { @Override public void setUp() throws Exception { super.setUp(); } @Override public void tearDown() throws Exception { super.tearDown(); } public void testMessageNumLimit() throws Exception { String secret = "muqwejlaksdfkljaliqwejflkasdfs"; String token = "jhlkasdfkjhasdfqwkasdfjqkwjhas"; Message msg = MessageUtil.create(500); for (int i = 0; i < 300; i++) { client.produce(secret, "erpDemoProduce-Consume", msg, token); } } } ================================================ FILE: client/src/test/java/com/messagebus/client/feature/ThriftWithAMQPRpc.java ================================================ package com.messagebus.client.feature; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.WrappedRpcServer; import com.messagebus.client.core.BaseTestCase; import com.messagebus.client.extension.thrift.TAMQPClientTransport; import com.messagebus.client.extension.thrift.ThriftMessageHandler; import com.messagebus.common.TestVariableInfo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TJSONProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.protocol.TProtocolFactory; import org.apache.thrift.transport.TTransport; /** * Created by yanghua on 4/17/15. */ public class ThriftWithAMQPRpc extends BaseTestCase { private static final Log logger = LogFactory.getLog(ThriftWithAMQPRpc.class); @Override public void setUp() throws Exception { super.setUp(); } @Override public void tearDown() throws Exception { super.tearDown(); } public void testThriftRpc() throws Exception { TTransport transport = new TAMQPClientTransport(this.client, "kliwhiduhaiucvarkjajksdbfkjabw", "emapDemoRpcResponse", "klasehnfkljashdnflhkjahwlekdjf", 10000); transport.open(); TProtocol protocol = new TJSONProtocol(transport); CalcService.Client client = new CalcService.Client(protocol); int result = client.calcSum(); logger.info(result); transport.close(); } public static void main(String[] args) { String zkConnectionStr = TestVariableInfo.ZK_CONNECTION_STRING; MessagebusSinglePool singlePool = new MessagebusSinglePool(zkConnectionStr); Messagebus client = singlePool.getResource(); //server code WrappedRpcServer rpcServer = null; try { TProcessor processor = new CalcService.Processor(new CalcServiceImpl()); TProtocolFactory inProtocolFactory = new TJSONProtocol.Factory(); TProtocolFactory outProtocolFactory = new TJSONProtocol.Factory(); rpcServer = client.buildRpcServer("mshdfjbqwejhfgasdfbjqkygaksdfa", new ThriftMessageHandler(processor, inProtocolFactory, outProtocolFactory)); rpcServer.mainLoop(); } finally { rpcServer.close(); singlePool.returnResource(client); singlePool.destroy(); } } } ================================================ FILE: common-component/pom.xml ================================================ messagebus com.messagebus 1.0.5 4.0.0 common jar org.apache.httpcomponents httpclient 4.2.1 org.xerial.snappy snappy-java 1.1.1.7 com.ning compress-lzf 1.0.3 ================================================ FILE: common-component/src/main/java/com/messagebus/common/AuthInfo.java ================================================ package com.messagebus.common; import java.io.Serializable; public class AuthInfo implements Serializable { private String userName; private String password; public AuthInfo() { } public AuthInfo(String userName, String password) { this.userName = userName; this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "AuthInfo{" + "userName='" + userName + '\'' + ", password='" + password + '\'' + '}'; } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/Constants.java ================================================ package com.messagebus.common; import java.nio.charset.Charset; /** * User: yanghua * Date: 6/29/14 * Time: 9:11 PM * Copyright (c) 2013 yanghua. All rights reserved. */ public class Constants { public static final Byte[] EMPTY_BYTE_ARRAY = new Byte[0]; public static final byte[] EMPTY_PRIMITIVE_BYTE_ARRAY = new byte[0]; public static final String PROXY_EXCHANGE_NAME = "exchange.proxy"; public static final String DEFAULT_FILE_QUEUE_NAME = "queue.proxy.log.file"; public static final String DEFAULT_CONFIG_RPC_RESPONSE_ROUTING_KEY = "routingkey.proxy.message.rpc.configRpcResponse"; public static final Charset CHARSET_OF_UTF8 = Charset.forName("UTF-8"); public static final long DEFAULT_DATACENTER_ID_FOR_UUID = 00001L; public static final String MESSAGE_HEADER_KEY_COMPRESS_ALGORITHM = "compressor"; } ================================================ FILE: common-component/src/main/java/com/messagebus/common/Event.java ================================================ package com.messagebus.common; import java.io.Serializable; /** * Created by yanghua on 6/24/15. */ public class Event implements Serializable { private String description; public Event() { } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Event{" + "description='" + description + '\'' + '}'; } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/ExceptionHelper.java ================================================ package com.messagebus.common; import org.apache.commons.logging.Log; import java.io.PrintWriter; import java.io.StringWriter; public class ExceptionHelper { public static String extractStackTrace(Throwable t) { if (t == null) { return ""; } StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { t.printStackTrace(pw); return sw.toString(); } finally { pw.close(); } } public static void logException(final Log logger, Throwable t, String additionalInfo) { if (additionalInfo != null) logger.error(additionalInfo); logger.error("-*- stacktrace -*- : " + extractStackTrace(t)); logger.error("-*- course -*- : " + extractStackTrace(t.getCause())); } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/GsonUtil.java ================================================ package com.messagebus.common; import com.google.gson.*; import java.lang.reflect.Type; import java.math.BigDecimal; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by yanghua on 4/1/15. */ public class GsonUtil { /** * convert json-object string to map * * @param jsonObjStr the string representation of json-object * @return the map object */ public static Map jsonStrToMap(String jsonObjStr) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Object.class, new NaturalDeserializer()); Gson gson = gsonBuilder.create(); return gson.fromJson(jsonObjStr, Map.class); } public static List jsonStrToList(String jsonObjStr) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Object.class, new NaturalDeserializer()); Gson gson = gsonBuilder.create(); return gson.fromJson(jsonObjStr, List.class); } /** * inner static class : implement JsonDeserializer interface * which overrides the default implementation */ private static class NaturalDeserializer implements JsonDeserializer { @Override public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { if (jsonElement.isJsonNull()) return null; else if (jsonElement.isJsonPrimitive()) return handlePrimitive(jsonElement.getAsJsonPrimitive()); else if (jsonElement.isJsonArray()) return handleArray(jsonElement.getAsJsonArray(), context); else return handleObject(jsonElement.getAsJsonObject(), context); } private Object handlePrimitive(JsonPrimitive json) { if (json.isBoolean()) return json.getAsBoolean(); else if (json.isString()) return json.getAsString(); else { BigDecimal bigDec = json.getAsBigDecimal(); // Find out if it is an int type try { bigDec.toBigIntegerExact(); try { return bigDec.intValueExact(); } catch (ArithmeticException e) { } return bigDec.longValue(); } catch (ArithmeticException e) { } // Just return it as a double return bigDec.doubleValue(); } } private Object handleArray(JsonArray json, JsonDeserializationContext context) { Object[] array = new Object[json.size()]; for (int i = 0; i < array.length; i++) array[i] = context.deserialize(json.get(i), Object.class); return array; } private Object handleObject(JsonObject json, JsonDeserializationContext context) { Map map = new HashMap(); for (Map.Entry entry : json.entrySet()) map.put(entry.getKey(), context.deserialize(entry.getValue(), Object.class)); return map; } } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/HttpHelper.java ================================================ package com.messagebus.common; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Map; /** * the util class for http request */ public class HttpHelper { private static final Log logger = LogFactory.getLog(HttpHelper.class); public static String syncHTTPGet(Map requestParamDic, AuthInfo authInfo) { HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = null; try { URI uri = new URIBuilder() .setScheme("http") .setHost(requestParamDic.get("host").toString()) .setPort(Integer.parseInt(requestParamDic.get("port").toString())) .setPath(requestParamDic.get("path").toString()) .setUserInfo(authInfo.getUserName(), authInfo.getPassword()) .build(); HttpGet httpGet = new HttpGet(uri); response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { long len = entity.getContentLength(); if (len == -1) return ""; if (len < 2 * 1024 * 1024) { return EntityUtils.toString(entity); } else { logger.error("[syncHTTPGet] response length is too large : (" + len + ") B " + "; and the url is : " + uri.getRawPath()); return ""; } } } catch (IOException e) { ExceptionHelper.logException(logger, e, "syncHTTPGet"); throw new RuntimeException(e); } catch (URISyntaxException e) { ExceptionHelper.logException(logger, e, "syncHTTPGet"); throw new RuntimeException(e); } return ""; } public static void asyncHTTPGet(String urlStr, AuthInfo authInfo, IAsyncCallback callback) { } public static void syncHTTPPut(String urlStr, AuthInfo authInfo) { } public static void asyncHTTPPut(String urlStr, AuthInfo authInfo, IAsyncCallback callback) { } public static void syncHTTPPost(String urlStr, AuthInfo authInfo) { } public static void asyncHTTPPost(String urlStr, AuthInfo authInfo, IAsyncCallback callback) { } public static void syncHTTPDelete(String urlStr, AuthInfo authInfo) { } public static void asyncHTTPDelete(String urlStr, AuthInfo authInfo, IAsyncCallback callback) { } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/IAsyncCallback.java ================================================ package com.messagebus.common; public interface IAsyncCallback { public void onResponse(String data); } ================================================ FILE: common-component/src/main/java/com/messagebus/common/InnerNoticeEntity.java ================================================ package com.messagebus.common; import java.io.Serializable; /** * Created by yanghua on 10/28/15. */ public class InnerNoticeEntity implements Serializable { private String identifier; private String value; private String type; public InnerNoticeEntity() { } public String getIdentifier() { return identifier; } public void setIdentifier(String identifier) { this.identifier = identifier; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getType() { return type; } public void setType(String type) { this.type = type; } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/NumberHelper.java ================================================ package com.messagebus.common; import java.text.NumberFormat; public class NumberHelper { public static double fractionDigits(double original, int digitNum) { NumberFormat format = NumberFormat.getNumberInstance(); format.setMaximumFractionDigits(digitNum); String tmp = format.format(original); tmp = tmp.replace(",", ""); return Double.valueOf(tmp); } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/RandomHelper.java ================================================ package com.messagebus.common; import java.util.Random; public class RandomHelper { /** * generate random character and number mixed sequence with a given

length

* * @param length the sequence's length that will be generated * @return generated sequence */ public static String randomNumberAndCharacter(int length) { StringBuilder sb = new StringBuilder(); Random rand = new Random(); Random randdata = new Random(); int data = 0; for (int i = 0; i < length; i++) { int index = rand.nextInt(3); switch (index) { case 0: data = randdata.nextInt(10); sb.append(data); break; case 1: data = randdata.nextInt(26) + 65; sb.append((char) data); break; case 2: data = randdata.nextInt(26) + 97; sb.append((char) data); break; default: break; } } return sb.toString(); } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/RouterType.java ================================================ package com.messagebus.common; import java.util.HashMap; import java.util.Map; /** * Desc: the definition of exchange type * more detail see : http://lostechies.com/derekgreer/2012/03/28/rabbitmq-for-windows-exchange-types/ * User: yanghua * Date: 6/29/14 * Time: 8:53 AM * Copyright (c) 2013 yanghua. All rights reserved. */ public enum RouterType { DIRECT, FANOUT, TOPIC, HEADERS; private static final Map lookups = new HashMap(4); static { lookups.put("direct", DIRECT); lookups.put("fanout", FANOUT); lookups.put("topic", TOPIC); lookups.put("headers", HEADERS); } public static String fromEnum(RouterType item) { for (Map.Entry entry : lookups.entrySet()) { if (entry.getValue().equals(item)) return entry.getKey(); } throw new IllegalArgumentException("illegal param : " + item.toString()); } public static RouterType lookup(String produceModeStr) { for (Map.Entry entry : lookups.entrySet()) { if (entry.getKey().equals(produceModeStr)) return entry.getValue(); } throw new IllegalArgumentException("illegal param : " + produceModeStr + " . can not fetch a enum item ."); } @Override public String toString() { for (Map.Entry item : lookups.entrySet()) { if (this.equals(item.getValue())) { return item.getKey(); } } throw new UnknownError("unknown Enum item "); } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/ShellHelper.java ================================================ package com.messagebus.common; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; public class ShellHelper { private static final Log logger = LogFactory.getLog(ShellHelper.class); public static ExecResult exec(String cmdStr) throws IOException, InterruptedException { String[] cmd = {"/bin/sh", "-c", cmdStr}; Process process = Runtime.getRuntime().exec(cmd); process.waitFor(); String errStr = translateFromStream(process.getErrorStream()); String infoStr = translateFromStream(process.getInputStream()); ExecResult execResult = new ExecResult(); execResult.setError(errStr); execResult.setInfo(infoStr); logger.debug("[exec] occurs a error, " + " and error msg is :" + errStr + " command is : " + cmdStr); logger.debug("[exec] output info is " + infoStr); return execResult; } private static String translateFromStream(InputStream stream) throws IOException { BufferedInputStream bufferedInputStream = new BufferedInputStream(stream); StringBuilder sb = new StringBuilder(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = bufferedInputStream.read(buffer)) != -1) { sb.append(new String(buffer, 0, bytesRead)); } return sb.toString(); } public static class ExecResult { private String info; private String error; public ExecResult() { } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getError() { return error; } public void setError(String error) { this.error = error; } @Override public String toString() { return "ExecResult{" + "info='" + info + '\'' + ", error='" + error + '\'' + '}'; } } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/TestVariableInfo.java ================================================ package com.messagebus.common; /** * Created by yanghua on 4/7/15. */ public class TestVariableInfo { public static String ZK_CONNECTION_STRING = "127.0.0.1:2181"; } ================================================ FILE: common-component/src/main/java/com/messagebus/common/UUIDGenerator.java ================================================ package com.messagebus.common; /** * base on twitter's Snowflake */ public class UUIDGenerator { private long workerId; private long datacenterId; private long sequence = 0L; private long twepoch = 1288834974657L; private long workerIdBits = 5L; private long datacenterIdBits = 5L; private long maxWorkerId = -1L ^ (-1L << workerIdBits); private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); private long sequenceBits = 12L; private long workerIdShift = sequenceBits; private long datacenterIdShift = sequenceBits + workerIdBits; private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; private long sequenceMask = -1L ^ (-1L << sequenceBits); private long lastTimestamp = -1L; public UUIDGenerator(long workerId, long datacenterId) { // sanity check for workerId if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } public synchronized long nextId() { long timestamp = timeGen(); if (timestamp < lastTimestamp) { throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = tilNextMillis(lastTimestamp); } } else { sequence = 0L; } lastTimestamp = timestamp; return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence; } protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } protected long timeGen() { return System.currentTimeMillis(); } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/compress/CompressEnum.java ================================================ package com.messagebus.common.compress; import java.util.HashMap; import java.util.Map; /** * Created by yanghua on 5/23/15. */ public enum CompressEnum { SNAPPY, LZF, LZ4; private static Map lookupMaps = new HashMap(3); static { lookupMaps.put("snappy", SNAPPY); lookupMaps.put("lzf", LZF); lookupMaps.put("lz4", LZ4); } private CompressEnum() { } public static CompressEnum lookup(String strVal) { if (strVal == null || strVal.isEmpty()) { throw new RuntimeException("param : strVal can not be null or empty"); } return lookupMaps.get(strVal.toLowerCase()); } public static String stringVal(CompressEnum compressEnum) { if (compressEnum == null) { throw new NullPointerException("param : compressEnum can not be null"); } for (Map.Entry entry : lookupMaps.entrySet()) { if (compressEnum.equals(entry.getValue())) { return entry.getKey(); } } return ""; } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/compress/CompressorFactory.java ================================================ package com.messagebus.common.compress; /** * Created by yanghua on 6/18/15. */ public final class CompressorFactory { public static ICompressor createCompressor(String algorName) { CompressEnum compressEnum = CompressEnum.lookup(algorName); switch (compressEnum) { case SNAPPY: return new SnappyCompressor(); case LZF: return new LZFCompressor(); default: throw new UnsupportedOperationException("unsupported compress mode : " + compressEnum.name()); } } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/compress/ICompressor.java ================================================ package com.messagebus.common.compress; import java.nio.charset.Charset; /** * Created by yanghua on 6/18/15. */ public interface ICompressor { public byte[] compress(byte[] source); public byte[] uncompress(byte[] target); public byte[] compressString(String source); public byte[] compressString(String source, Charset charset); public String uncompressString(byte[] target); public String uncompressString(byte[] target, Charset charset); public byte[] compressStream(byte[] source); public byte[] uncompressStream(byte[] target); } ================================================ FILE: common-component/src/main/java/com/messagebus/common/compress/LZFCompressor.java ================================================ package com.messagebus.common.compress; import com.ning.compress.lzf.*; import java.io.*; import java.nio.charset.Charset; /** * LZF compressor * more detail : https://github.com/ning/compress */ class LZFCompressor implements ICompressor { public byte[] compress(byte[] source) { return LZFEncoder.encode(source); } public byte[] uncompress(byte[] target) { try { return LZFDecoder.decode(target); } catch (LZFException e) { throw new RuntimeException(e); } } public byte[] compressString(String source) { byte[] strBytes = source.getBytes(); return LZFEncoder.encode(strBytes); } public byte[] compressString(String source, Charset charset) { byte[] strBytes = source.getBytes(charset); return LZFEncoder.encode(strBytes); } public String uncompressString(byte[] target) { try { byte[] sourceBytes = LZFDecoder.decode(target); return new String(sourceBytes); } catch (LZFException e) { throw new RuntimeException(e); } } public String uncompressString(byte[] target, Charset charset) { try { byte[] sourceBytes = LZFDecoder.decode(target); return new String(sourceBytes, charset); } catch (LZFException e) { throw new RuntimeException(e); } } public byte[] compressStream(byte[] source) { OutputStream compressStream = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); compressStream = new LZFOutputStream(baos); compressStream.write(source); compressStream.flush(); byte[] compressed = baos.toByteArray(); return compressed; } catch (IOException e) { throw new RuntimeException(e); } finally { try { if (compressStream != null) { compressStream.close(); } } catch (IOException e) { } } } public byte[] uncompressStream(byte[] target) { byte[] bytes = null; InputStream uncompressedStream = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ByteArrayInputStream bais = new ByteArrayInputStream(target); uncompressedStream = new LZFCompressingInputStream(bais); int num = -1; byte[] buf = new byte[1024]; baos = new ByteArrayOutputStream(); while ((num = uncompressedStream.read(buf, 0, buf.length)) != -1) { baos.write(bytes, 0, num); } return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { if (uncompressedStream != null) { uncompressedStream.close(); } baos.close(); } catch (IOException e) { } } } } ================================================ FILE: common-component/src/main/java/com/messagebus/common/compress/SnappyCompressor.java ================================================ package com.messagebus.common.compress; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.xerial.snappy.Snappy; import org.xerial.snappy.SnappyInputStream; import org.xerial.snappy.SnappyOutputStream; import java.io.*; import java.nio.charset.Charset; /** * snappy compressor * more detail : https://github.com/xerial/snappy-java */ class SnappyCompressor implements ICompressor { private static final Log logger = LogFactory.getLog(SnappyCompressor.class); public byte[] compress(byte[] source) { try { return Snappy.compress(source); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } } public byte[] uncompress(byte[] target) { try { return Snappy.uncompress(target); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } } public byte[] compressString(String source) { try { return Snappy.compress(source); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } } public byte[] compressString(String source, Charset charset) { try { return Snappy.compress(source, charset); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } } public String uncompressString(byte[] target) { try { return Snappy.uncompressString(target); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } } public String uncompressString(byte[] target, Charset charset) { try { return Snappy.uncompressString(target, charset); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } } public byte[] compressStream(byte[] source) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream stream = new SnappyOutputStream(baos); byte[] compressed; try { stream.write(source); stream.flush(); compressed = baos.toByteArray(); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } finally { try { stream.close(); } catch (IOException e) { } } return compressed; } public byte[] uncompressStream(byte[] target) { ByteArrayOutputStream baos = null; InputStream inputStream = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(target); inputStream = new SnappyInputStream(bais); byte[] buf = new byte[1024]; int num = -1; baos = new ByteArrayOutputStream(); while ((num = inputStream.read(buf, 0, buf.length)) != -1) { baos.write(buf, 0, num); } return baos.toByteArray(); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } finally { try { if (inputStream != null) { inputStream.close(); } if (baos != null) { baos.close(); } } catch (IOException e) { } } } } ================================================ FILE: common-component/src/main/resources/log4j.properties ================================================ # Configure logging for testing log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.encoding=utf-8 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n ================================================ FILE: httpbridge/README.md ================================================ #Restful API ##produce: ``` /messagebus/queues/{qname}/messages?appkey={appkey}&type={produce} ``` http method : `POST` request params : * path : qname - queue name * querystring : * appkey - auth key (must) * type - identify API,value `produce` (must) * request body : * messages - message object list (must) response : ```js { statusCode: 10200, error: "", msg: "", data: '' } ``` ##consume: ``` /messagebus/queues/{qname}/messages?appkey={appkey}&type={consume}&mode={sync}&num={num} ``` http method : `GET` request params : * path : qname - queue name * querystring : * appkey - auth key (must) * type - identify API,value `consume` (must) * mode - value `sync` or `async` (must) * num - except num,from 0 < num to 100(equals) (mode must be sync) response : ```js { statusCode: 10200, error: "", msg: "", data: [ { messageHeader: { messageId: 520133271997313000, type: "appMessage", timestamp: null, priority: 0, expiration: null, deliveryMode: 2, headers: null, contentEncoding: null, contentType: null, replyTo: null, appId: null, userId: null, clusterId: null, correlationId: null }, messageBody: { messageBody: [ 116, 101, 115, 116 ] }, messageType: "AppMessage" } ] } ``` ##request: ``` /messagebus/queues/{qname}/messages?appkey={appkey}&type={type}&timeout={timeout} ``` http method : `POST` request params : * path : qname - queue name * querystring : * appkey - auth key(must) * type - identify API,value `request`(must) * timeout - timeout,unit microsecond(must) * request body : * message - message object (client blocked and just once) response : ```js { statusCode: 10200, error: "", msg: "", data: { messageHeader: { messageId: 520133271997313000, type: "appMessage", timestamp: null, priority: 0, expiration: null, deliveryMode: 2, headers: null, contentEncoding: null, contentType: null, replyTo: null, appId: null, userId: null, clusterId: null, correlationId: null }, messageBody: { messageBody: [ 116, 101, 115, 116 ] }, messageType: "AppMessage" } } ``` ##response: ``` /messagebus/queues/{qname}/messages?appkey={appkey} ``` http method : `POST` request params : * path : qname - queue name * querystring : * appkey - auth key(must) * type - identify API,value `response`(must) * request body : * message - message object response : ```js { statusCode: 10200, error: "", msg: "", data: '' } ``` ================================================ FILE: httpbridge/pom.xml ================================================ messagebus com.messagebus 1.0.5 4.0.0 httpbridge war 1.0.5 com.messagebus client 1.0.5 javax.servlet javax.servlet-api 3.1.0 org.eclipse.jetty jetty-continuation 9.3.0.M0 httpbridge maven-compiler-plugin 2.3.2 true 1.6 1.6 UTF-8 org.apache.maven.plugins maven-surefire-plugin true ================================================ FILE: httpbridge/src/main/java/com/messagebus/httpbridge/controller/ExceptionHandler.java ================================================ package com.messagebus.httpbridge.controller; import com.messagebus.httpbridge.util.Constants; import com.messagebus.httpbridge.util.ResponseUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by yanghua on 3/31/15. */ public class ExceptionHandler extends HttpServlet { private static final Log logger = LogFactory.getLog(ExceptionHandler.class); @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uri = (String) req.getAttribute("javax.servlet.error.request_uri"); Object exception = req.getAttribute("javax.servlet.error.exception"); logger.error("error uri : " + uri); logger.error("error type : " + exception); ResponseUtil.response(resp, Constants.HTTP_FAILED_CODE, exception.toString(), "", "\"\""); } } ================================================ FILE: httpbridge/src/main/java/com/messagebus/httpbridge/controller/HttpBridge.java ================================================ package com.messagebus.httpbridge.controller; import com.google.common.base.Strings; import com.messagebus.client.*; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.client.message.model.MessageJSONSerializer; import com.messagebus.client.model.MessageCarryType; import com.messagebus.httpbridge.util.CommonUtil; import com.messagebus.httpbridge.util.Constants; import com.messagebus.httpbridge.util.ResponseUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.jetty.continuation.Continuation; import org.eclipse.jetty.continuation.ContinuationListener; import org.eclipse.jetty.continuation.ContinuationSupport; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; public class HttpBridge extends HttpServlet { private static final Log logger = LogFactory.getLog(HttpBridge.class); private static final String CONSUME_MODE_PULL = "pull"; private static final String CONSUME_MODE_PUSH = "push"; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.info("[service] url is : " + req.getRequestURI()); String apiType = req.getParameter("apiType"); if (Strings.isNullOrEmpty(apiType)) { ResponseUtil.response(resp, Constants.HTTP_FAILED_CODE, "the query string : type can not be null or empty", "", "\"\""); return; } String secret = req.getParameter("secret"); if (Strings.isNullOrEmpty(secret)) { ResponseUtil.response(resp, Constants.HTTP_FAILED_CODE, "param : secret can not be null or empty", "", "\"\""); return; } MessageCarryType msgCarryType = MessageCarryType.lookup(apiType); switch (msgCarryType) { case PRODUCE: this.produce(req, resp); break; case CONSUME: this.consume(req, resp); break; case PUBLISH: this.publish(req, resp); break; case SUBSCRIBE: this.subscribe(req, resp); break; case REQUEST: this.request(req, resp); break; case RESPONSE: ResponseUtil.response(resp, Constants.HTTP_FAILED_CODE, "unsupported type : " + msgCarryType.toString(), "", "\"\""); break; default: ResponseUtil.response(resp, Constants.HTTP_FAILED_CODE, "invalidate type", "", "\"\""); } } private void produce(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String httpMethod = request.getMethod().toLowerCase(); if (httpMethod.equals("get")) { produceWithGet(request, response); } else if (httpMethod.equals("post")) { produceWithPost(request, response); } } private void produceWithGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!request.getMethod().toLowerCase().equals("get")) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "error http request method", "", "\"\""); return; } String callback = request.getParameter("callback"); if (Strings.isNullOrEmpty(callback)) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "param : callback can not be null or empty", "", "\"\""); return; } String queueName = request.getRequestURI().split("/")[3]; String token = request.getParameter("token"); if (Strings.isNullOrEmpty(queueName)) { ResponseUtil.responseForJsonp(response, callback, Constants.HTTP_FAILED_CODE, "param : qname can not be null or empty", "", "\"\""); return; } if (Strings.isNullOrEmpty(token)) { ResponseUtil.responseForJsonp(response, callback, Constants.HTTP_FAILED_CODE, "param : token can not be null or empty", "", "\"\""); return; } String contentEncoding = request.getParameter("contentEncoding"); if (Strings.isNullOrEmpty(contentEncoding)) { contentEncoding = "UTF-8"; } String contentType = request.getParameter("contentType"); if (Strings.isNullOrEmpty(contentType)) { ResponseUtil.responseForJsonp(response, callback, Constants.HTTP_FAILED_CODE, "param : contentType can not be null or empty", "", "\"\""); return; } if (!CommonUtil.validMessageType(contentType)) { ResponseUtil.responseForJsonp(response, callback, Constants.HTTP_FAILED_CODE, "param : contentType should be : " + Constants.TEXT_PLAIN_CONTENT_TYPE, "", "\"\""); return; } String replyTo = request.getParameter("replyTo"); if (Strings.isNullOrEmpty(replyTo)) { replyTo = queueName; } String appId = request.getParameter("appId"); if (Strings.isNullOrEmpty(appId)) { appId = ""; } String correlationId = request.getParameter("correlationId"); if (Strings.isNullOrEmpty(correlationId)) { correlationId = queueName; } String content = request.getParameter("content"); if (Strings.isNullOrEmpty(content)) { content = ""; } Message msg = MessageFactory.createMessage(); msg.setTimestamp(new Date().getTime()); msg.setContentEncoding(contentEncoding); msg.setContentType(contentType); msg.setReplyTo(replyTo); msg.setAppId(appId); msg.setCorrelationId(correlationId); msg.setContent(content.getBytes()); MessagebusPool pool = (MessagebusPool) (getServletContext().getAttribute(Constants.KEY_OF_MESSAGEBUS_POOL_OBJ)); Messagebus messagebus = pool.getResource(); try { messagebus.produce(request.getParameter("secret"), queueName, msg, token); ResponseUtil.responseForJsonp(response, callback, Constants.HTTP_SUCCESS_CODE, "", "", "\"\""); } catch (Exception e) { ResponseUtil.responseForJsonp(response, callback, Constants.HTTP_FAILED_CODE, "[produce] occurs a exception : " + e.getMessage(), "", "\"\""); } finally { pool.returnResource(messagebus); } } private void produceWithPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String queueName = request.getRequestURI().split("/")[3]; String token = request.getParameter("token"); String msgArrStr = request.getParameter("messages"); if (Strings.isNullOrEmpty(queueName)) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "param : qname can not be null or empty", "", "\"\""); return; } if (Strings.isNullOrEmpty(token)) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "param : token can not be null or empty", "", "\"\""); return; } if (Strings.isNullOrEmpty(msgArrStr)) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "param : messages can not be null or empty", "", "''"); return; } Message[] msgArr = MessageJSONSerializer.deSerializeMessages(msgArrStr); boolean mergedValidResult = true; for (Message msg : msgArr) { mergedValidResult = mergedValidResult && CommonUtil.validMessageType(msg.getContentType()); if (!mergedValidResult) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "invalid message content type", "", "\"\""); return; } } MessagebusPool pool = (MessagebusPool) (getServletContext().getAttribute(Constants.KEY_OF_MESSAGEBUS_POOL_OBJ)); Messagebus messagebus = pool.getResource(); try { messagebus.batchProduce(request.getParameter("secret"), queueName, msgArr, token); ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", "\"\""); } catch (Exception e) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "[produce] occurs a exception : " + e.getMessage(), "", "\"\""); } finally { pool.returnResource(messagebus); } } private void consume(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!request.getMethod().toLowerCase().equals("get")) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "error http request method", "", "\"\""); return; } String mode = request.getParameter("mode"); if (Strings.isNullOrEmpty(mode)) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "the param : mode can not be null or empty", "", "\"\""); return; } String lowerMode = mode.toLowerCase(); if (lowerMode.equals(CONSUME_MODE_PULL)) { this.consumeWithPull(request, response); } else if (lowerMode.equals(CONSUME_MODE_PUSH)) { this.consumeWithPush(request, response); } else { logger.error("[consume] invalidate param : mode with value - " + mode); } } private void publish(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!request.getMethod().toLowerCase().equals("post")) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "error http request method", "", ""); return; } String msgArrStr = request.getParameter("messages"); Message[] msgArr = MessageJSONSerializer.deSerializeMessages(msgArrStr); boolean mergedValidResult = true; for (Message msg : msgArr) { mergedValidResult = CommonUtil.validMessageType(msg.getContentType()); if (!mergedValidResult) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "invalid message content type", "", "\"\""); return; } } MessagebusPool pool = (MessagebusPool) (getServletContext().getAttribute(Constants.KEY_OF_MESSAGEBUS_POOL_OBJ)); Messagebus messagebus = pool.getResource(); try { messagebus.publish(request.getParameter("secret"), msgArr); ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", "\"\""); } catch (Exception e) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "[produce] occurs a exception : " + e.getMessage(), "", "\"\""); } finally { pool.returnResource(messagebus); } } private void subscribe(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { if (!request.getMethod().toLowerCase().equals("get")) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "error http request method", "", "\"\""); return; } final Continuation continuation = ContinuationSupport.getContinuation(request); if (continuation.isExpired()) { ResponseUtil.response(response, Constants.HTTP_TIMEOUT_CODE, "timeout", "there is no message could be consumed in " + Constants.MAX_CONSUME_CONTINUATION_TIMEOUT + " ms", "\"\""); return; } continuation.setTimeout(Constants.MAX_CONSUME_CONTINUATION_TIMEOUT); continuation.suspend(response); final MessagebusPool pool = (MessagebusPool) (getServletContext().getAttribute(Constants.KEY_OF_MESSAGEBUS_POOL_OBJ)); final Messagebus messagebus = pool.getResource(); final List receivedMsgs = new ArrayList(); continuation.addContinuationListener(new ContinuationListener() { @Override public void onComplete(Continuation continuation) { try { if (receivedMsgs.size() != 0) { String msgStr = MessageJSONSerializer.serializeMessages(receivedMsgs); ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", msgStr); } } catch (IOException e) { logger.error("[onComplete] occurs a IOException : " + e.getMessage()); } } @Override public void onTimeout(Continuation continuation) { try { if (receivedMsgs.size() == 0) { ResponseUtil.response(response, Constants.HTTP_TIMEOUT_CODE, "", "", "\"\""); } else { String msgStr = MessageJSONSerializer.serializeMessages(receivedMsgs); ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", msgStr); } } catch (IOException e) { logger.error("[onTimeout] occurs a IOException : " + e.getMessage()); } } }); try { messagebus.subscribe( request.getParameter("secret"), request.getParameter("from"), request.getParameter("token"), new IMessageReceiveListener() { @Override public void onMessage(Message message) { receivedMsgs.add(message); } }, Constants.MAX_CONSUME_CONTINUATION_TIMEOUT, TimeUnit.MILLISECONDS); } catch (Exception e) { logger.error("[consumeWithPush] occurs a Exception : " + e.getMessage()); continuation.undispatch(); ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "[consumeWithPush] occurs a Exception : " + e.getMessage(), "", "\"\""); } finally { pool.returnResource(messagebus); continuation.complete(); } } private void request(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!request.getMethod().toLowerCase().equals("post")) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "error request method !", "", "\"\""); return; } String timeoutStr = request.getParameter("timeout"); if (Strings.isNullOrEmpty(timeoutStr)) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "param : timeout can not be null or empty", "", "\"\""); return; } long timeout; try { timeout = Long.parseLong(timeoutStr); } catch (NumberFormatException e) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "illegal param : timeout ", "", "\"\""); return; } if (timeout < Constants.MIN_REQUEST_TIMEOUT || timeout > Constants.MAX_REQUEST_TIMEOUT) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "invalid param : timeout it should be greater than :" + Constants.MIN_REQUEST_TIMEOUT + "and less than : " + Constants.MAX_REQUEST_TIMEOUT, "", "\"\""); return; } String token = request.getParameter("token"); if (Strings.isNullOrEmpty(token)) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "param : token can not be null or empty", "", "\"\""); return; } String queueName = request.getRequestURI().split("/")[3]; String msgStr = request.getParameter("message"); if (Strings.isNullOrEmpty(msgStr)) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "param : msgStr can not be null or empty", "", "\"\""); return; } Message msg = MessageJSONSerializer.deSerialize(msgStr); MessagebusPool pool = (MessagebusPool) (getServletContext().getAttribute(Constants.KEY_OF_MESSAGEBUS_POOL_OBJ)); Messagebus messagebus = pool.getResource(); try { Message responseMsg = messagebus.request(request.getParameter("secret"), queueName, msg, token, timeout); String respMsgStr = MessageJSONSerializer.serialize(responseMsg); ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", respMsgStr); } catch (MessagebusUnOpenException e) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "occurs a messagebus unopen exception", "", "\"\""); } catch (MessageResponseTimeoutException e) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "occurs a response timeout exception", "", "\"\""); } catch (Exception e) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "occurs a exception : " + e.getMessage(), "", "\"\""); } finally { pool.returnResource(messagebus); } } @Deprecated private void response(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { throw new UnsupportedOperationException("unsupported operation!"); } private void consumeWithPull(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String numStr = request.getParameter("num"); if (Strings.isNullOrEmpty(numStr)) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "when consume with pull mode the param : num can not be null or empty", "", "''"); return; } int num = 0; try { num = Integer.parseInt(numStr); } catch (NumberFormatException e) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "invalidate param : num, it must be a integer!", "", "''"); return; } if (num < Constants.MIN_CONSUME_NUM || num > Constants.MAX_CONSUME_NUM) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, " invalidate param : num , it should be less than " + Constants.MAX_CONSUME_NUM + " and greater than " + Constants.MIN_CONSUME_NUM, "", "''"); return; } MessagebusPool pool = (MessagebusPool) (getServletContext().getAttribute(Constants.KEY_OF_MESSAGEBUS_POOL_OBJ)); Messagebus messagebus = pool.getResource(); List messages = null; try { messages = messagebus.consume(request.getParameter("secret"), num); } catch (Exception e) { ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "", "", "[]"); return; } finally { pool.returnResource(messagebus); } if (messages == null) { ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", "[]"); } else { String msgsStr = MessageJSONSerializer.serializeMessages(messages); ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", msgsStr); } } private void consumeWithPush(HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { final Continuation continuation = ContinuationSupport.getContinuation(request); if (continuation.isExpired()) { ResponseUtil.response(response, Constants.HTTP_TIMEOUT_CODE, "timeout", "there is no message could be consumed in " + Constants.MAX_CONSUME_CONTINUATION_TIMEOUT + " ms", "[]"); return; } continuation.setTimeout(Constants.MAX_CONSUME_CONTINUATION_TIMEOUT); continuation.suspend(response); final MessagebusPool pool = (MessagebusPool) (getServletContext().getAttribute(Constants.KEY_OF_MESSAGEBUS_POOL_OBJ)); final Messagebus messagebus = pool.getResource(); final List receivedMsgs = new ArrayList(); continuation.addContinuationListener(new ContinuationListener() { @Override public void onComplete(Continuation continuation) { try { if (receivedMsgs.size() != 0) { String msgStr = MessageJSONSerializer.serializeMessages(receivedMsgs); ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", msgStr); } else { ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", "[]"); } } catch (IOException e) { logger.error("[onComplete] occurs a IOException : " + e.getMessage()); } } @Override public void onTimeout(Continuation continuation) { try { if (receivedMsgs.size() == 0) { ResponseUtil.response(response, Constants.HTTP_TIMEOUT_CODE, "", "", "''"); } else { String msgStr = MessageJSONSerializer.serializeMessages(receivedMsgs); ResponseUtil.response(response, Constants.HTTP_SUCCESS_CODE, "", "", msgStr); } } catch (IOException e) { logger.error("[onTimeout] occurs a IOException : " + e.getMessage()); } } }); try { messagebus.consume(request.getParameter("secret"), Constants.MAX_CONSUME_CONTINUATION_TIMEOUT, TimeUnit.MILLISECONDS, new IMessageReceiveListener() { @Override public void onMessage(Message message) { receivedMsgs.add(message); } }); } catch (Exception e) { logger.error("[consumeWithPush] occurs a Exception : " + e.getMessage()); continuation.undispatch(); ResponseUtil.response(response, Constants.HTTP_FAILED_CODE, "[consumeWithPush] occurs a Exception : " + e.getMessage(), "", "''"); } finally { pool.returnResource(messagebus); continuation.complete(); } } } ================================================ FILE: httpbridge/src/main/java/com/messagebus/httpbridge/filter/URLDispatcher.java ================================================ package com.messagebus.httpbridge.filter; import com.google.common.base.Strings; import com.google.gson.Gson; import com.messagebus.httpbridge.util.Constants; import com.messagebus.httpbridge.util.ResponseUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by yanghua on 9/30/14. */ public class URLDispatcher implements Filter { private static final Log logger = LogFactory.getLog(URLDispatcher.class); private static final Gson gson = new Gson(); private static final String URI_PREFIX = "/messagebus/queues"; private static final String ERROR_URI_PERFIX = "/error"; private static final String KEY_OF_SECRET = "secret"; private static final String KEY_OF_TYPE = "apiType"; @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; String uri = request.getRequestURI(); if (uri.startsWith(ERROR_URI_PERFIX)) { filterChain.doFilter(request, servletResponse); } if (!uri.startsWith(URI_PREFIX)) { logger.error("[doFilter] request uri is " + request.getRequestURI()); ResponseUtil.response((HttpServletResponse) servletResponse, Constants.HTTP_NOT_FOUND_CODE, "the request uri : " + request.getRequestURI() + "is not found!", "", gson.toJson("")); } else { String appKeyVal = request.getParameter(KEY_OF_SECRET); String type = request.getParameter(KEY_OF_TYPE); if (Strings.isNullOrEmpty(appKeyVal)) { logger.error("[doFilter] missed query string : " + KEY_OF_SECRET); ResponseUtil.response((HttpServletResponse) servletResponse, Constants.HTTP_FAILED_CODE, "missed query string : " + KEY_OF_SECRET + "!", "", gson.toJson("")); } else if (Strings.isNullOrEmpty(type)) { logger.error("[doFilter] missed query string : " + KEY_OF_TYPE); ResponseUtil.response((HttpServletResponse) servletResponse, Constants.HTTP_FAILED_CODE, "missed query string : " + KEY_OF_TYPE + "!", "", gson.toJson("")); } else { filterChain.doFilter(request, servletResponse); } } } @Override public void destroy() { } } ================================================ FILE: httpbridge/src/main/java/com/messagebus/httpbridge/listener/AppContextListener.java ================================================ package com.messagebus.httpbridge.listener; import com.messagebus.client.MessagebusPool; import com.messagebus.httpbridge.util.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class AppContextListener implements ServletContextListener { private static final Log logger = LogFactory.getLog(AppContextListener.class); @Override public void contextInitialized(ServletContextEvent servletContextEvent) { ServletContext servletContext = servletContextEvent.getServletContext(); String pubsuberHost = servletContext.getInitParameter("pubsuberHost"); int pubsuberPort = Integer.parseInt(servletContext.getInitParameter("pubsuberPort")); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(Integer.parseInt(servletContext.getInitParameter("messagebuspool.maxtotal"))); MessagebusPool messagebusPool = new MessagebusPool(pubsuberHost, poolConfig); servletContextEvent.getServletContext().setAttribute(Constants.KEY_OF_MESSAGEBUS_POOL_OBJ, messagebusPool); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { MessagebusPool messagebusPool = (MessagebusPool) servletContextEvent.getServletContext().getAttribute(Constants.KEY_OF_MESSAGEBUS_POOL_OBJ); if (messagebusPool != null) { messagebusPool.destroy(); } } } ================================================ FILE: httpbridge/src/main/java/com/messagebus/httpbridge/model/TextMessage.java ================================================ package com.messagebus.httpbridge.model; import java.util.Date; import java.util.Map; /** * Created by yanghua on 3/31/15. */ public class TextMessage { //header private long messageId; private String type; private Date timestamp; private short priority; private String expiration; private Map headers; private String contentEncoding; private String contentType; private String replyTo; private String appId; private String userId; private String clusterId; private String correlationId; private short deliveryMode = 2; private String content; public TextMessage() {} public long getMessageId() { return messageId; } public void setMessageId(long messageId) { this.messageId = messageId; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Date getTimestamp() { return timestamp; } public void setTimestamp(Date timestamp) { this.timestamp = timestamp; } public short getPriority() { return priority; } public void setPriority(short priority) { this.priority = priority; } public String getExpiration() { return expiration; } public short getDeliveryMode() { return 2; } public Map getHeaders() { return headers; } public void setHeaders(Map headers) { this.headers = headers; } public String getContentEncoding() { return contentEncoding; } public void setContentEncoding(String contentEncoding) { this.contentEncoding = contentEncoding; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } public String getReplyTo() { return replyTo; } public void setReplyTo(String replyTo) { this.replyTo = replyTo; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getUserId() { return userId; } protected void setUserId(String userId) { this.userId = null; } public String getClusterId() { return clusterId; } protected void setClusterId(String clusterId) { this.clusterId = null; } public String getCorrelationId() { return correlationId; } public void setCorrelationId(String correlationId) { this.correlationId = correlationId; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } ================================================ FILE: httpbridge/src/main/java/com/messagebus/httpbridge/util/CommonUtil.java ================================================ package com.messagebus.httpbridge.util; import com.google.common.base.Strings; /** * Created by yanghua on 3/30/15. */ public class CommonUtil { public static boolean validMessageType(String msgTypeStr) { if (Strings.isNullOrEmpty(msgTypeStr)) { return false; } return msgTypeStr.equals(Constants.TEXT_PLAIN_CONTENT_TYPE); } } ================================================ FILE: httpbridge/src/main/java/com/messagebus/httpbridge/util/Constants.java ================================================ package com.messagebus.httpbridge.util; public class Constants { //region response status code private static final int HTTP_BASE_CODE = 10000; public static final int HTTP_SUCCESS_CODE = HTTP_BASE_CODE + 200; public static final int HTTP_FAILED_CODE = HTTP_BASE_CODE + 500; public static final int HTTP_INVALID_PARAM_CODE = HTTP_BASE_CODE + 501; public static final int HTTP_TIMEOUT_CODE = HTTP_BASE_CODE + 502; public static final int HTTP_NOT_FOUND_CODE = HTTP_BASE_CODE + 404; //endregion public static final String KEY_OF_MESSAGEBUS_POOL_OBJ = "messagebusPool"; //consume mode pull public static final int MAX_CONSUME_NUM = 500; public static final int MIN_CONSUME_NUM = 1; //consume mode push public static final long MAX_CONSUME_CONTINUATION_TIMEOUT = 30000L; //request timeout public static final long MAX_REQUEST_TIMEOUT = 60000L; public static final long MIN_REQUEST_TIMEOUT = 100L; public static final String TEXT_PLAIN_CONTENT_TYPE = "text/plain"; } ================================================ FILE: httpbridge/src/main/java/com/messagebus/httpbridge/util/ResponseUtil.java ================================================ package com.messagebus.httpbridge.util; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.Charset; public class ResponseUtil { public static void response(HttpServletResponse response, int statusCode, String err, String msg, String data) throws IOException { response.setCharacterEncoding("utf-8"); response.setContentType("application/json"); String responseStr = "{ \"statusCode\" : " + statusCode + "," + " \"error\" : \"" + err + "\"," + " \"msg\" : \"" + msg + "\", " + " \"data\" : " + data + "}"; response.setContentLength(responseStr.getBytes(Charset.defaultCharset()).length); PrintWriter writer = response.getWriter(); writer.write(responseStr); writer.flush(); writer.close(); } public static void responseForJsonp(HttpServletResponse response, String callback, int statusCode, String err, String msg, String data) throws IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/javascript"); String responseStr = callback + "({ \"statusCode\" : " + statusCode + "," + " \"error\" : \"" + err + "\"," + " \"msg\" : \"" + msg + "\", " + " \"data\" : " + data + "})"; response.setContentLength(responseStr.getBytes(Charset.defaultCharset()).length); PrintWriter writer = response.getWriter(); writer.write(responseStr); writer.flush(); writer.close(); } } ================================================ FILE: httpbridge/src/main/java/com/messagebus/httpbridge/util/TextMessageJSONSerializer.java ================================================ package com.messagebus.httpbridge.util; import com.google.gson.*; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import com.messagebus.client.message.model.Message; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.Collection; /** * Created by yanghua on 3/30/15. */ public class TextMessageJSONSerializer { private static final Log logger = LogFactory.getLog(TextMessageJSONSerializer.class); private static final Gson gson = new GsonBuilder() .serializeNulls() .registerTypeAdapter(byte[].class, new TextMessageJSONSerializer.ByteArrAdapter()) .create(); public static String serialize(Message msg) { return gson.toJson(msg); } public static Message deSerialize(String msgStr) { JsonParser parser = new JsonParser(); JsonElement element = parser.parse(msgStr); Message msg = gson.fromJson(element, Message.class); return msg; } public static Message deSerialize(JsonElement msgElement) { JsonObject obj = msgElement.getAsJsonObject(); Message msg = gson.fromJson(obj.get("message"), Message.class); return msg; } public static String serializeMessages(Collection msgs) { return gson.toJson(msgs); } public static Message[] deSerializeMessages(String msgArrStr) { int i = 0; JsonParser parser = new JsonParser(); JsonElement element = parser.parse(msgArrStr); if (!element.isJsonArray()) { logger.error("unsupported original data. it should be a string of json object array "); throw new UnsupportedOperationException("unsupported original data. " + "it should be a string of json object array "); } Message[] msgs = new Message[element.getAsJsonArray().size()]; for (JsonElement e : element.getAsJsonArray()) { Message msg = gson.fromJson(e, Message.class); msgs[i++] = msg; } return msgs; } private static class ByteArrAdapter extends TypeAdapter { @Override public void write(JsonWriter jsonWriter, byte[] bytes) throws IOException { if (bytes == null) { jsonWriter.nullValue(); } else { jsonWriter.value(new String(bytes)); } } @Override public byte[] read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() == null) { return null; } String tmp = jsonReader.nextString(); if (tmp == null) return new byte[0]; return tmp.getBytes(); } } } ================================================ FILE: httpbridge/src/main/resources/log4j.properties ================================================ # Configure logging for testing log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.encoding=utf-8 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n ================================================ FILE: httpbridge/src/main/webapp/WEB-INF/jetty-web.xml ================================================ / ================================================ FILE: httpbridge/src/main/webapp/WEB-INF/web.xml ================================================ Message Bus Http Bridge pubsuberHost 202.195.244.146 pubsuberPort 6379 messagebuspool.maxtotal 200 httpBridge com.messagebus.httpbridge.controller.HttpBridge true exception com.messagebus.httpbridge.controller.ExceptionHandler httpBridge /messagebus/* exception /error urlDispatcher com.messagebus.httpbridge.filter.URLDispatcher true urlDispatcher /* com.messagebus.httpbridge.listener.AppContextListener java.lang.Exception /error ================================================ FILE: httpbridge/src/test/java/com/messagebus/httpbridge/util/PropertiesHelper.java ================================================ package com.messagebus.httpbridge.util; import java.io.IOException; import java.util.Properties; /** * Created by yanghua on 3/25/15. */ public class PropertiesHelper { private static final Properties commonProperties; static { commonProperties = new Properties(); try { commonProperties.load(commonProperties.getClass().getClassLoader().getResourceAsStream("common.properties")); } catch (IOException e) { e.printStackTrace(); } } public static String getPropertyValue(String key) { return commonProperties.getProperty(key); } } ================================================ FILE: httpbridge/src/test/resource/common.properties ================================================ messagebus.pubsuber.host=172.16.206.250 messagebus.pubsuber.port=6379 messagebus.httpbridge.host=172.16.206.29 messagebus.httpbridge.port=8080 ================================================ FILE: interactor-component/pom.xml ================================================ messagebus com.messagebus 1.0.5 4.0.0 interactor-component jar com.messagebus common 1.0.5 messagebus-client maven-compiler-plugin 2.3.2 true 1.6 1.6 UTF-8 ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/proxy/ProxyConsumer.java ================================================ package com.messagebus.interactor.proxy; import com.rabbitmq.client.Channel; import com.rabbitmq.client.GetResponse; import com.rabbitmq.client.QueueingConsumer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; public class ProxyConsumer { private static final Log logger = LogFactory.getLog(ProxyConsumer.class); public static QueueingConsumer consume(Channel channel, String queueName, boolean autoAck, String consumerTag) throws IOException { QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, autoAck, consumerTag, consumer); return consumer; } public static GetResponse consumeSingleMessage(Channel channel, String queueName) throws IOException { return channel.basicGet(queueName, true); } } ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/proxy/ProxyProducer.java ================================================ package com.messagebus.interactor.proxy; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import java.io.IOException; import java.util.List; public class ProxyProducer { public static void produce(String exchangeName, Channel channel, String routingKey, byte[] data, AMQP.BasicProperties properties ) throws IOException { channel.basicPublish(exchangeName, routingKey, properties, data); } public static void produceWithTX(String exchangeName, Channel channel, String routingKey, byte[] data, AMQP.BasicProperties properties ) throws IOException { //transaction begin channel.txSelect(); channel.basicPublish(exchangeName, routingKey, properties, data); //commit every message with wrapped a transaction //NOTE: it is almost for security! Not for normal, because of bad performance!!! channel.txCommit(); } public static void batchProduce(String exchangeName, Channel channel, String routingKey, List dataList, AMQP.BasicProperties properties ) throws IOException { for (byte[] bytes : dataList) produce(exchangeName, channel, routingKey, bytes, properties); } public static void batchProduceWithTX(String exchangeName, Channel channel, String routingKey, List dataList, AMQP.BasicProperties properties ) throws IOException { for (byte[] bytes : dataList) produceWithTX(exchangeName, channel, routingKey, bytes, properties); } } ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/rabbitmq/AbstractInitializer.java ================================================ package com.messagebus.interactor.rabbitmq; import com.google.common.base.Strings; import com.rabbitmq.client.Address; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; public abstract class AbstractInitializer { private static final Log logger = LogFactory.getLog(AbstractInitializer.class); protected Connection connection; protected Channel channel; protected String mqConnectionStr; protected AbstractInitializer(String mqConnectionStr) { this.mqConnectionStr = mqConnectionStr; } protected void init() { if (Strings.isNullOrEmpty(mqConnectionStr)) { logger.info("can not get mq connection info"); throw new RuntimeException("can not get mq connection info"); } String[] hostPortPairArr = mqConnectionStr.split(","); Address[] addresses = new com.rabbitmq.client.Address[hostPortPairArr.length]; for (int i = 0; i < hostPortPairArr.length; i++) { String[] hostPortArr = hostPortPairArr[i].split(":"); com.rabbitmq.client.Address address = new com.rabbitmq.client.Address( hostPortArr[0], Integer.parseInt(hostPortArr[1]) ); addresses[i] = address; } ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setAutomaticRecoveryEnabled(true); connectionFactory.setTopologyRecoveryEnabled(true); connectionFactory.setConnectionTimeout(60000); connectionFactory.setRequestedHeartbeat(10); try { this.connection = connectionFactory.newConnection(addresses); this.channel = this.connection.createChannel(); } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } catch (TimeoutException e) { logger.error(e); throw new RuntimeException(e); } catch (Exception e) { logger.error(e); throw new RuntimeException(e); } } protected void close() { try { if (this.channel != null && this.channel.isOpen()) { this.channel.close(); } if (this.connection != null && this.connection.isOpen()) { this.connection.close(); } } catch (IOException e) { logger.error("close exception ", e); throw new RuntimeException("close exception ", e); } catch (TimeoutException e) { logger.error("close exception ", e); throw new RuntimeException("close exception ", e); } catch (Exception e) { logger.error("close exception ", e); throw new RuntimeException("close exception ", e); } } } ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/rabbitmq/Exchange.java ================================================ package com.messagebus.interactor.rabbitmq; import java.io.Serializable; /** * Created by yanghua on 11/20/15. */ public class Exchange implements Serializable { private int id; private String name; private String exchangeName; private int parentId; private String routerType; private String routingKey; private String description; public Exchange() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getExchangeName() { return exchangeName; } public void setExchangeName(String exchangeName) { this.exchangeName = exchangeName; } public int getParentId() { return parentId; } public void setParentId(int parentId) { this.parentId = parentId; } public String getRouterType() { return routerType; } public void setRouterType(String routerType) { this.routerType = routerType; } public String getRoutingKey() { return routingKey; } public void setRoutingKey(String routingKey) { this.routingKey = routingKey; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/rabbitmq/ExchangeManager.java ================================================ package com.messagebus.interactor.rabbitmq; import com.rabbitmq.client.Channel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; public class ExchangeManager extends AbstractInitializer { private static Log logger = LogFactory.getLog(ExchangeManager.class); private static volatile ExchangeManager instance; private ExchangeManager(String host) { super(host); } public static ExchangeManager defaultManager(String host) { if (instance == null) { synchronized (ExchangeManager.class) { if (instance == null) { instance = new ExchangeManager(host); } } } return instance; } public void create(String exchangeName, String routerType) throws IOException { super.init(); this.channel.exchangeDeclare(exchangeName, routerType, true); super.close(); } public void create(String exchangeName, String routerType, String bindTo, String routingKey) throws IOException { super.init(); this.channel.exchangeDeclare(exchangeName, routerType, true); //bind if (bindTo != null && !bindTo.isEmpty() && this.innerExists(bindTo, channel)) this.channel.exchangeBind(exchangeName, bindTo, routingKey); super.close(); } public void bind(String exchangeName, String bindTo, String routingKey) throws IOException { super.init(); if (!this.innerExists(exchangeName, channel) || !this.innerExists(bindTo, channel)) { logger.error("exchange : " + exchangeName + " or " + bindTo + "is not exists"); throw new IOException("exchange : " + exchangeName + " or " + bindTo + "is not exists"); } this.channel.exchangeBind(exchangeName, bindTo, routingKey); super.close(); } public void unbind(String exchangeName, String unbindTo, String routingKey) throws IOException { super.init(); if (!this.innerExists(exchangeName, channel) || !this.innerExists(unbindTo, channel)) { logger.error("exchange : " + exchangeName + " or " + unbindTo + "is not exists"); throw new IOException("exchange : " + exchangeName + " or " + unbindTo + "is not exists"); } this.channel.exchangeUnbind(exchangeName, unbindTo, routingKey); super.close(); } public void delete(String exchangeName) throws IOException { super.init(); if (!this.innerExists(exchangeName, channel)) { logger.error("exchange : " + exchangeName + " is not exists"); throw new IOException("exchange : " + exchangeName + " is not exists"); } this.channel.exchangeDelete(exchangeName); super.close(); } public boolean exchangeExists(String exchangeName) throws IOException { super.init(); boolean result = true; try { this.channel.exchangeDeclarePassive(exchangeName); } catch (IOException e) { result = false; } super.close(); return result; } private boolean innerExists(String exchangeName, Channel outerChannel) { boolean result = true; try { outerChannel.exchangeDeclarePassive(exchangeName); } catch (IOException e) { result = false; } return result; } } ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/rabbitmq/MQDataInitializer.java ================================================ package com.messagebus.interactor.rabbitmq; import com.rabbitmq.client.AMQP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; public class MQDataInitializer extends AbstractInitializer { private static Log logger = LogFactory.getLog(MQDataInitializer.class); private static volatile MQDataInitializer instance = null; private MQDataInitializer(String mqConnectionStr) { super(mqConnectionStr); } public static MQDataInitializer getInstance(String mqConnectionStr) { if (instance == null) { synchronized (MQDataInitializer.class) { if (instance == null) { instance = new MQDataInitializer(mqConnectionStr); } } } return instance; } public void deleteQueueNoWait(String queueName) { try { super.init(); AMQP.Queue.DeleteOk deleteOk = channel.queueDelete(queueName); if (deleteOk == null) { throw new RuntimeException("delete queue with name : " + queueName + " failed."); } } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } finally { super.close(); } } public void initExchange(List sortedExchanges, Map exchangeMap) { try { super.init(); //declare exchange for (Exchange exchange : sortedExchanges) { channel.exchangeDeclare(exchange.getExchangeName(), exchange.getRouterType(), true); } //bind exchange for (Exchange exchange : sortedExchanges) { if (exchange.getParentId() == -1) continue; channel.exchangeBind(exchange.getExchangeName(), exchangeMap.get(exchange.getParentId()).getExchangeName(), exchange.getRoutingKey()); } } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } finally { super.close(); } } public void initQueue(List queues) { try { super.init(); //declare queue for (Queue queue : queues) { Map queueConfig = new HashMap(2); if (queue.getThreshold() != -1) { queueConfig.put("x-max-length", queue.getThreshold()); } if (queue.getThreshold() != -1 && queue.getMsgBodySize() != -1) { int allMsgSize = queue.getThreshold() * queue.getMsgBodySize(); queueConfig.put("x-max-length-bytes", allMsgSize); } if (queue.getTtl() != -1) { channel.queueDelete(queue.getQueueName()); queueConfig.put("x-expires", queue.getTtl()); } if (queue.getTtlPerMsg() != -1) { channel.queueDelete(queue.getQueueName()); queueConfig.put("x-message-ttl", queue.getTtlPerMsg()); } channel.queueDeclare(queue.getQueueName(), true, false, false, queueConfig); } //bind queue for (Queue queue : queues) { channel.queueBind(queue.getQueueName(), queue.getBindExchange(), queue.getRoutingKey()); } } catch (IOException e) { logger.error(e); throw new RuntimeException(e); } finally { super.close(); } } private void destroyTopologyComponent() throws IOException { //call reset-app } private boolean exchangeExists(String exchangeName) throws IOException { boolean result = true; try { channel.exchangeDeclarePassive(exchangeName); } catch (IOException e) { result = false; if (!channel.isOpen()) { super.init(); } } return result; } private boolean queueExists(String queueName) throws IOException { boolean result = true; try { channel.queueDeclarePassive(queueName); } catch (IOException e) { result = false; if (!channel.isOpen()) { super.init(); } } return result; } } ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/rabbitmq/Queue.java ================================================ package com.messagebus.interactor.rabbitmq; import java.io.Serializable; /** * Created by yanghua on 11/20/15. */ public class Queue implements Serializable { private String queueName; private String bindExchange; private String routingKey; private String typeId; private int threshold; private int msgBodySize; private int ttl; private int ttlPerMsg; public Queue() { } public String getQueueName() { return queueName; } public void setQueueName(String queueName) { this.queueName = queueName; } public String getBindExchange() { return bindExchange; } public void setBindExchange(String bindExchange) { this.bindExchange = bindExchange; } public String getRoutingKey() { return routingKey; } public void setRoutingKey(String routingKey) { this.routingKey = routingKey; } public String getTypeId() { return typeId; } public void setTypeId(String typeId) { this.typeId = typeId; } public int getThreshold() { return threshold; } public void setThreshold(int threshold) { this.threshold = threshold; } public int getMsgBodySize() { return msgBodySize; } public void setMsgBodySize(int msgBodySize) { this.msgBodySize = msgBodySize; } public int getTtl() { return ttl; } public void setTtl(int ttl) { this.ttl = ttl; } public int getTtlPerMsg() { return ttlPerMsg; } public void setTtlPerMsg(int ttlPerMsg) { this.ttlPerMsg = ttlPerMsg; } } ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/rabbitmq/QueueManager.java ================================================ package com.messagebus.interactor.rabbitmq; import com.rabbitmq.client.Channel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; public class QueueManager extends AbstractInitializer { private static final Log logger = LogFactory.getLog(QueueManager.class); private static volatile QueueManager instance = null; private QueueManager(String host) { super(host); } public static QueueManager defaultQueueManager(String host) { if (instance == null) { synchronized (QueueManager.class) { if (instance == null) instance = new QueueManager(host); } } return instance; } public void create(String queueName) throws IOException { super.init(); if (queueName.isEmpty()) { logger.error("[create] queueName param is empty"); throw new IOException("[create] queueName param is empty"); } this.channel.queueDeclare(queueName, true, false, false, null); super.close(); } public void create(String queueName, String bindTo, String routingKey) throws IOException { super.init(); if (queueName.isEmpty()) { logger.error("[create] queueName param is empty"); throw new IOException("[create] queueName param is empty"); } this.channel.queueDeclare(queueName, true, false, false, null); if (bindTo != null && !bindTo.isEmpty() && this.innerExists(bindTo, this.channel)) this.channel.queueBind(queueName, bindTo, routingKey); super.close(); } public void bind(String queueName, String bindTo, String routingKey) throws IOException { super.init(); if (!this.innerExists(queueName, this.channel)) { logger.error("[bind] queue : " + queueName + " is not exists!"); throw new IOException("queue : " + queueName + " is not exists!"); } if (!this.innerExists(bindTo, this.channel)) { logger.error("[bind] bindTo : " + bindTo + " is not exists!"); throw new IOException("[bind] bindTo : " + bindTo + " is not exists!"); } this.channel.queueBind(queueName, bindTo, routingKey); super.close(); } public void unbind(String queueName, String unbindTo, String routingKey) throws IOException { super.init(); if (queueName.isEmpty()) { logger.error("[unbind] queueName is empty"); throw new IOException("[unbind] queueName is empty"); } if (unbindTo.isEmpty()) { logger.error("[ubind] unbindTo param is empty"); throw new IOException("[ubind] unbindTo param is empty"); } if (!this.innerExists(queueName, this.channel)) { logger.error("[unbind] queue : " + queueName + " is not exists"); } if (!this.innerExists(unbindTo, this.channel)) { logger.error("[unbind] unbind queue : " + unbindTo + " is not exists"); } this.channel.queueUnbind(queueName, unbindTo, routingKey); super.close(); } public void delete(String queueName) throws IOException { super.init(); if (queueName.isEmpty()) { logger.error("[delete] queueName is empty"); throw new IOException("[delete] queueName is empty"); } if (!this.innerExists(queueName, this.channel)) { logger.error("[delete] queue : " + queueName + " is not exists"); } this.channel.queueDelete(queueName); super.close(); } public boolean exists(String queueName) throws IOException { super.init(); boolean result = true; try { this.channel.queueDeclarePassive(queueName); } catch (IOException e) { result = false; } super.close(); return result; } /** * for other function, it borrow a outer channel * * @param queueName * @param outerChannel * @return */ private boolean innerExists(String queueName, Channel outerChannel) { boolean result = true; try { outerChannel.queueDeclarePassive(queueName); } catch (IOException e) { result = false; } return result; } } ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/rabbitmq/RabbitmqServerManager.java ================================================ package com.messagebus.interactor.rabbitmq; import com.messagebus.common.ExceptionHelper; import com.messagebus.interactor.util.ShellHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.IOException; import java.net.Inet4Address; import java.net.UnknownHostException; public class RabbitmqServerManager { private static final Log logger = LogFactory.getLog(RabbitmqServerManager.class); public static void start(String mqHost) { if (!comeFromSameHost(mqHost)) { throw new RuntimeException("the message bus server's host and the mq server's host" + " are not the same"); } logger.info("starting mq server..."); String mqServerStartupCmdStr = "rabbitmq-server start"; try { ShellHelper.exec("/usr/sbin/service " + mqServerStartupCmdStr); } catch (IOException e) { ExceptionHelper.logException(logger, e, "start"); throw new RuntimeException(e); } catch (InterruptedException e) { } } public static void stop(String mqHost) { if (!comeFromSameHost(mqHost)) { throw new RuntimeException("the message bus server's host and the mq server's host" + " are not the same"); } logger.info("stopping mq server..."); String mqServerStopCmdStr = "rabbitmq-server stop"; try { ShellHelper.exec("/usr/sbin/service " + mqServerStopCmdStr); } catch (IOException e) { logger.error("occurs an IOException : " + e.getMessage()); throw new RuntimeException("occurs an IOException : " + e.getMessage()); } catch (InterruptedException e) { } } public static boolean isAlive(String mqHost) { if (!comeFromSameHost(mqHost)) { throw new RuntimeException("the message bus server's host and the mq server's host" + " are not the same"); } try { ShellHelper.ExecResult result = ShellHelper.exec("rabbitmqctl status | grep 'pid'"); return result.getInfo().contains("pid"); } catch (IOException e) { logger.error("[isAlive] occurs IOException : " + e.getMessage()); return false; } catch (InterruptedException e) { return false; } } private static boolean comeFromSameHost(String host) { try { String ipStr = Inet4Address.getLocalHost().getHostAddress(); return ipStr.equals(host); } catch (UnknownHostException e) { logger.error("unknown host exception : " + e.toString()); throw new RuntimeException("unknown host exception : " + e.toString()); } } } ================================================ FILE: interactor-component/src/main/java/com/messagebus/interactor/util/ShellHelper.java ================================================ package com.messagebus.interactor.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; public class ShellHelper { private static final Log logger = LogFactory.getLog(ShellHelper.class); public static ExecResult exec(String cmdStr) throws IOException, InterruptedException { String[] cmd = {"/bin/sh", "-c", cmdStr}; Process process = Runtime.getRuntime().exec(cmd); process.waitFor(); String errStr = translateFromStream(process.getErrorStream()); String infoStr = translateFromStream(process.getInputStream()); ExecResult execResult = new ExecResult(); execResult.setError(errStr); execResult.setInfo(infoStr); logger.debug("[exec] occurs a error, " + " and error msg is :" + errStr + " command is : " + cmdStr); logger.debug("[exec] output info is " + infoStr); return execResult; } private static String translateFromStream(InputStream stream) throws IOException { BufferedInputStream bufferedInputStream = new BufferedInputStream(stream); StringBuilder sb = new StringBuilder(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = bufferedInputStream.read(buffer)) != -1) { sb.append(new String(buffer, 0, bytesRead)); } return sb.toString(); } public static class ExecResult { private String info; private String error; public ExecResult() { } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getError() { return error; } public void setError(String error) { this.error = error; } @Override public String toString() { return "ExecResult{" + "info='" + info + '\'' + ", error='" + error + '\'' + '}'; } } } ================================================ FILE: managesystem/banyan_DB.sql ================================================ CREATE DATABASE IF NOT EXISTS `banyan_DB` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */; USE `banyan_DB`; -- MySQL dump 10.13 Distrib 5.6.17, for osx10.6 (i386) -- -- Host: 172.16.206.17 Database: banyan_DB -- ------------------------------------------------------ -- Server version 5.6.19-0ubuntu0.14.04.1 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `APP` -- DROP TABLE IF EXISTS `APP`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `APP` ( `APP_ID` varchar(20) NOT NULL, `NAME` varchar(100) DEFAULT NULL, `CREATOR` varchar(20) DEFAULT NULL, `FROM_DATE` datetime DEFAULT NULL, `THRU_DATE` datetime DEFAULT NULL, `LAST_UPDATED_STAMP` datetime DEFAULT NULL, `LAST_UPDATED_TX_STAMP` datetime DEFAULT NULL, `CREATED_STAMP` datetime DEFAULT NULL, `CREATED_TX_STAMP` datetime DEFAULT NULL, PRIMARY KEY (`APP_ID`), KEY `fk_App_UserLogin` (`CREATOR`), KEY `APP_TXSTMP` (`LAST_UPDATED_TX_STAMP`), KEY `APP_TXCRTS` (`CREATED_TX_STAMP`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `APP` -- LOCK TABLES `APP` WRITE; /*!40000 ALTER TABLE `APP` DISABLE KEYS */; INSERT INTO `APP` VALUES ('1','server','admin','2015-03-22 09:15:45',NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('100','cms','banyanAdmin','2015-03-28 10:15:45',NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('101','oa','banyanAdmin','2015-03-28 10:15:45',NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('2','console','admin','2015-03-22 09:15:45',NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('3','emap','banyanAdmin','2015-03-16 10:15:45',NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('4','erp','banyanAdmin','2015-03-16 10:15:45',NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('5','amp','banyanAdmin','2015-03-31 03:28:30',NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('6','logSys','banyanAdmin','2015-04-27 13:28:30',NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('7','remoteAmp','banyanAdmin','2015-04-28 20:10:30',NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'); /*!40000 ALTER TABLE `APP` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `AUDIT_TYPE` -- DROP TABLE IF EXISTS `AUDIT_TYPE`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `AUDIT_TYPE` ( `AUDIT_TYPE_CODE` varchar(100) NOT NULL, `AUDIT_TYPE_NAME` varchar(100) DEFAULT NULL, `LAST_UPDATED_STAMP` datetime DEFAULT NULL, `LAST_UPDATED_TX_STAMP` datetime DEFAULT NULL, `CREATED_STAMP` datetime DEFAULT NULL, `CREATED_TX_STAMP` datetime DEFAULT NULL, PRIMARY KEY (`AUDIT_TYPE_CODE`), KEY `AUDIT_TYPE_TXSTMP` (`LAST_UPDATED_TX_STAMP`), KEY `AUDIT_TYPE_TXCRTS` (`CREATED_TX_STAMP`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `AUDIT_TYPE` -- LOCK TABLES `AUDIT_TYPE` WRITE; /*!40000 ALTER TABLE `AUDIT_TYPE` DISABLE KEYS */; INSERT INTO `AUDIT_TYPE` VALUES ('AUDIT_BEFORE','???','2015-04-28 19:48:16','2015-04-28 19:48:16','2015-04-28 19:47:17','2015-04-28 19:47:17'),('AUDIT_FAILED','?????','2015-04-28 19:48:16','2015-04-28 19:48:16','2015-04-28 19:47:17','2015-04-28 19:47:17'),('AUDIT_SUCCESS','????','2015-04-28 19:48:16','2015-04-28 19:48:16','2015-04-28 19:47:17','2015-04-28 19:47:17'); /*!40000 ALTER TABLE `AUDIT_TYPE` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `CONFIG` -- DROP TABLE IF EXISTS `CONFIG`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `CONFIG` ( `CONFIG_ID` varchar(20) NOT NULL, `ITEM_KEY` varchar(100) DEFAULT NULL, `ITEM_VALUE` varchar(255) DEFAULT NULL, `TYPE` varchar(255) DEFAULT NULL, `LAST_UPDATED_STAMP` datetime DEFAULT NULL, `LAST_UPDATED_TX_STAMP` datetime DEFAULT NULL, `CREATED_STAMP` datetime DEFAULT NULL, `CREATED_TX_STAMP` datetime DEFAULT NULL, PRIMARY KEY (`CONFIG_ID`), KEY `CONFIG_TXSTMP` (`LAST_UPDATED_TX_STAMP`), KEY `CONFIG_TXCRTS` (`CREATED_TX_STAMP`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `CONFIG` -- LOCK TABLES `CONFIG` WRITE; /*!40000 ALTER TABLE `CONFIG` DISABLE KEYS */; INSERT INTO `CONFIG` VALUES ('1','messagebus.client.host','172.16.206.29','client','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('2','messagebus.client.port','5672','client','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'); /*!40000 ALTER TABLE `CONFIG` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `NODE` -- DROP TABLE IF EXISTS `NODE`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `NODE` ( `NODE_ID` varchar(20) NOT NULL, `SECRET` varchar(255) DEFAULT NULL, `NAME` varchar(100) DEFAULT NULL, `VALUE` varchar(255) DEFAULT NULL, `PARENT_ID` varchar(20) DEFAULT NULL, `TYPE` varchar(20) DEFAULT NULL, `ROUTER_TYPE` varchar(255) DEFAULT NULL, `ROUTING_KEY` varchar(255) DEFAULT NULL, `AVAILABLE` char(1) DEFAULT NULL, `IS_INNER` char(1) DEFAULT NULL, `IS_VIRTUAL` char(1) DEFAULT NULL, `COMMUNICATE_TYPE` varchar(100) DEFAULT NULL, `CREATOR` varchar(20) DEFAULT NULL, `APP_ID` varchar(20) DEFAULT NULL, `RATE_LIMIT` varchar(60) DEFAULT NULL, `THRESHOLD` varchar(60) DEFAULT NULL, `MSG_BODY_SIZE` varchar(60) DEFAULT NULL, `TTL` varchar(60) DEFAULT NULL, `TTL_PER_MSG` varchar(60) DEFAULT NULL, `AUDIT_TYPE_CODE` varchar(100) DEFAULT NULL, `CAN_BROADCAST` char(1) DEFAULT NULL, `FROM_DATE` datetime DEFAULT NULL, `THRU_DATE` datetime DEFAULT NULL, `LAST_UPDATED_STAMP` datetime DEFAULT NULL, `LAST_UPDATED_TX_STAMP` datetime DEFAULT NULL, `CREATED_STAMP` datetime DEFAULT NULL, `CREATED_TX_STAMP` datetime DEFAULT NULL, PRIMARY KEY (`NODE_ID`), KEY `fk_Node_UserLogin` (`CREATOR`), KEY `fk_Node_AuditType` (`AUDIT_TYPE_CODE`), KEY `fk_Node_App` (`APP_ID`), KEY `NODE_TXSTMP` (`LAST_UPDATED_TX_STAMP`), KEY `NODE_TXCRTS` (`CREATED_TX_STAMP`), CONSTRAINT `fk_Node_App` FOREIGN KEY (`APP_ID`) REFERENCES `APP` (`APP_ID`), CONSTRAINT `fk_Node_AuditType` FOREIGN KEY (`AUDIT_TYPE_CODE`) REFERENCES `AUDIT_TYPE` (`AUDIT_TYPE_CODE`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `NODE` -- LOCK TABLES `NODE` WRITE; /*!40000 ALTER TABLE `NODE` DISABLE KEYS */; INSERT INTO `NODE` VALUES ('1',NULL,'proxy','exchange.proxy','-1','0','fanout',NULL,'1','1',NULL,NULL,'banyanAdmin',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('10','iqwjasdfklakqoiajsidfoasidjoqw','serverInfoResponse','queue.proxy.message.reqresp.serverInfoResponse','6','1',NULL,'routingkey.proxy.message.reqresp.serverInfoResponse','1','0','0','response','banyanAdmin','1',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('100','kjhqwehuksadhjkhkqfkjshduwuhas','cmsDemoProduce','queue.proxy.message.procon.cmsDemoProduce','4','1',NULL,'routingkey.proxy.message.procon.cmsDemoProduce','1','0','1','produce','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_BEFORE',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('101','qiuiuchvqihuchvqnuicoaudhfieqi','cmsDemoConsume','queue.proxy.message.procon.cmsDemoConsume','4','1',NULL,'routingkey.proxy.message.procon.cmsDemoConsume','1','0','0','consume','banyanAdmin','4','1000','20000','2000',NULL,NULL,'AUDIT_BEFORE',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('102','ijwlieulcaisduhfilhliuhdfbsjhx','cmsDemoPublish','queue.proxy.message.pubsub.cmsDemoPublish','5','1',NULL,'routingkey.proxy.message.pubsub.cmsDemoPublish','1','0','1','publish','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_BEFORE',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('103','jnsdlkblkAWEHLIDVBAJSDlohkjasn','cmsDemoSubscribe','queue.proxy.message.pubsub.cmsDemoSubscribe','5','1',NULL,'routingkey.proxy.message.pubsub.cmsDemoSubscribe','1','0','0','subscribe','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_BEFORE',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('104','mfqemwqkjdkuhxqnklhawjebuwyiud','oaDemoProduce','queue.proxy.message.procon.oaDemoProduce','4','1',NULL,'routingkey.proxy.message.procon.oaDemoProduce','1','0','1','produce','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_BEFORE',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('105','miwenasuhqiboudfqiuhfiuhdfquha','oaDemoConsume','queue.proxy.message.procon.oaDemoConsume','4','1',NULL,'routingkey.proxy.message.procon.oaDemoConsume','1','0','0','consume','banyanAdmin','4','1000','20000','2000',NULL,NULL,'AUDIT_BEFORE',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('106','ewoiasnflieunjkdflqienflsdifln','oaDemoPublish','queue.proxy.message.pubsub.oaDemoPublish','5','1',NULL,'routingkey.proxy.message.pubsub.oaDemoPublish','1','0','1','publish','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_BEFORE',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('107','mnxcblawkebflifiauhfqliwuhflan','oaDemoSubscribe','queue.proxy.message.pubsub.oaDemoSubscribe','5','1',NULL,'routingkey.proxy.message.pubsub.oaDemoSubscribe','1','0','0','subscribe','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_BEFORE',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('11','nadjfqulaudhfkauwaudhfakqajd','serverCmdResponse','queue.proxy.message.reqresp.serverCmdResponse','6','1',NULL,'routingkey.proxy.message.reqresp.serverCmdResponse','1','0','0','response','banyanAdmin','1',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('12','miuhqihusahdfuhaksjhfuiqweka','serverInfoRequest','queue.proxy.message.reqresp.serverInfoRequest','6','1',NULL,'routingkey.proxy.message.reqresp.serverInfoRequest','1','0','0','request','banyanAdmin','2',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('13','lauhsdjkfhqiuwequhiausdfhuah','serverCmdRequest','queue.proxy.message.reqresp.serverCmdRequest','6','1',NULL,'routingkey.proxy.message.reqresp.serverCmdRequest','1','0','0','request','banyanAdmin','2',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('2',NULL,'log','exchange.proxy.log','1','0','fanout',NULL,'1','1',NULL,NULL,'banyanAdmin',NULL,NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('200','iojawdnaisdflknoiankjfdblaidcas','appDataQueue','queue.proxy.message.procon.appDataQueue','4','1',NULL,'routingkey.proxy.message.procon.appDataQueue','1','0','0','produce-consume','banyanAdmin','5',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('201','qliudwnfljainfqlhalsdbfqlwehfbl','appErrorEventQueue','queue.proxy.message.procon.appErrorEventQueue','4','1',NULL,'routingkey.proxy.message.procon.appErrorEventQueue','1','0','0','consume','banyanAdmin','5',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('3',NULL,'message','exchange.proxy.message','1','0','topic',NULL,'1','1',NULL,NULL,'banyanAdmin',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('300','liredkifnhaqweuhfqbablwddflawea','appErrorEventProducer','queue.proxy.message.procon.appErrorEventProducer','4','1',NULL,'routingkey.proxy.message.procon.appErrorEventProducer','1','0','1','produce','banyanAdmin','6',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('4',NULL,'procon','exchange.proxy.message.procon','3','0','topic','routingkey.proxy.message.procon.#','1','1',NULL,NULL,'banyanAdmin',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('400','jbdlhbcLQHDBLASBDALsdblASHBDjhn','appErrorRemoteQProducer','queue.proxy.message.procon.appErrorRemoteQProducer','4','1',NULL,'routingkey.proxy.message.procon.appErrorRemoteQProducer','1','0','1','produce','banyanAdmin','7',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('401','lkaqnwejnfalwefjbsdfkjasnfjqlas','appErrorRemoteQ','queue.proxy.message.procon.appErrorRemoteQ','4','1',NULL,'routingkey.proxy.message.procon.appErrorRemoteQ','1','0','0','consume','banyanAdmin','7',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('43','kljasdoifqoikjhhhqwhebasdfasdf','erpDemoProduce','queue.proxy.message.procon.erpDemoProduce','4','1',NULL,'routingkey.proxy.message.procon.erpDemoProduce','1','0','1','produce','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS','1',NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('44','kjhasdfhlkuqjhgaebjhasgdfabfak','erpDemoConsume','queue.proxy.message.procon.erpDemoConsume','4','1',NULL,'routingkey.proxy.message.procon.erpDemoConsume','1','0','0','consume','banyanAdmin','4','1000','20000','2000',NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('45','jnmzqwemnjaksdfqjnkajfjasndfnw','erpDemoProduce-Consume','queue.proxy.message.procon.erpDemoProduce-Consume','4','1',NULL,'routingkey.proxy.message.procon.erpDemoProduce-Consume','1','0','0','produce-consume','banyanAdmin','4',NULL,'100','500',NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('46','iuoqiwejicaoisfaisfbsqewnfjnfa','erpDemoRequest','queue.proxy.message.reqresp.erpDemoRequest','6','1',NULL,'routingkey.proxy.message.reqresp.erpDemoRequest','1','0','1','request','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('47','muciasnajjkasbdfbaskjdfkjkasja','erpDemoResponse','queue.proxy.message.reqresp.erpDemoResponse','6','1',NULL,'routingkey.proxy.message.reqresp.erpDemoResponse','1','0','0','response','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('48','qiwernjknfaawenkjasdfkjnakllas','erpDemoRequest-Response','queue.proxy.message.reqresp.erpDemoRequest-Response','6','1',NULL,'routingkey.proxy.message.reqresp.erpDemoRequest-Response','1','0','0','request-response','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('49','oiqwenncuicnsdfuasdfnkajkwqowe','erpDemoPublish','queue.proxy.message.pubsub.erpDemoPublish','5','1',NULL,'routingkey.proxy.message.pubsub.erpDemoPublish','1','0','1','publish','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('5',NULL,'pubsub','exchange.proxy.message.pubsub','3','0','topic','routingkey.proxy.message.pubsub.#','1','1',NULL,NULL,'banyanAdmin',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('50','nckljsenlkjanefluiwnlanfmsdfas','erpDemoSubscribe','queue.proxy.message.pubsub.erpDemoSubscribe','5','1',NULL,'routingkey.proxy.message.pubsub.erpDemoSubscribe','1','0','0','subscribe','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('51','xzmjsdafnkjanuwqijpasdfnanlkna','erpDemoPublish-Subscribe','queue.proxy.message.pubsub.erpDemoPublish-Subscribe','5','1',NULL,'routingkey.proxy.message.pubsub.erpDemoPublish-Subscribe','1','0','0','publish-subscribe','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('52','kliwhiduhaiucvarkjajksdbfkjabw','erpDemoRpcRequest','queue.proxy.message.rpc.erpDemoRpcRequest','7','1',NULL,'routingkey.proxy.message.rpc.erpDemoRpcRequest','1','0','1','rpcrequest','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('53','jhliquwdlaisudfqbjhasdfulquias','erpDemoRpcResponse','queue.proxy.message.rpc.erpDemoRpcResponse','7','1',NULL,'routingkey.proxy.message.rpc.erpDemoRpcResponse','1','0','0','rpcresponse','banyanAdmin','4',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('6',NULL,'reqresp','exchange.proxy.message.reqresp','3','0','topic','routingkey.proxy.message.reqresp.#','1','1',NULL,NULL,'banyanAdmin',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('7',NULL,'rpc','exchange.proxy.message.rpc','3','0','topic','routingkey.proxy.message.rpc.#','1','1',NULL,NULL,'banyanAdmin',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('73','muqwejlaksdfkljaliqwejflkasdfs','emapDemoProduce','queue.proxy.message.procon.emapDemoProduce','4','1',NULL,'routingkey.proxy.message.procon.emapDemoProduce','1','0','1','produce','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('74','zxdjnflakwenklasjdflkqpiasdfnj','emapDemoConsume','queue.proxy.message.procon.emapDemoConsume','4','1',NULL,'routingkey.proxy.message.procon.emapDemoConsume','1','0','0','consume','banyanAdmin','3','2000',NULL,'1000',NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('75','zxjnlasndflkjasdlflqwekjnflaks','emapDemoProduce-Consume','queue.proxy.message.procon.emapDemoProduce-Consume','4','1',NULL,'routingkey.proxy.message.procon.emapDemoProduce-Consume','1','0','0','produce-consume','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('76','mzxhblawdblulawkwkkwksifhlaasd','emapDemoRequest','queue.proxy.message.reqresp.emapDemoRequest','6','1',NULL,'routingkey.proxy.message.reqresp.emapDemoRequest','1','0','1','request','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('77','zxjhvclawenlkfhsladfnqpwenflak','emapDemoResponse','queue.proxy.message.reqresp.emapDemoResponse','6','1',NULL,'routingkey.proxy.message.reqresp.emapDemoResponse','1','0','0','response','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('78','xcvnbladflkasudflwepqowiejfoas','emapDemoRequest-Response','queue.proxy.message.reqresp.emapDemoRequest-Response','6','1',NULL,'routingkey.proxy.message.reqresp.emapDemoRequest-Response','1','0','0','request-response','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('79','zxcjvhlawenlkfhsalfqpiieiwqqsd','emapDemoPublish','queue.proxy.message.pubsub.emapDemoPublish','5','1',NULL,'routingkey.proxy.message.pubsub.emapDemoPublish','1','0','1','publish','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('8',NULL,'notification','exchange.proxy.message.notification','3','0','fanout','routingkey.proxy.message.notification.#','1','1',NULL,NULL,'banyanAdmin',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('80','zxcnvblawelkusahdfqwiuhowefhnx','emapDemoSubscribe','queue.proxy.message.pubsub.emapDemoSubscribe','5','1',NULL,'routingkey.proxy.message.pubsub.emapDemoSubscribe','1','0','0','subscribe','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('81','zxcnvblaeflbwehiouhusduaflsuda','emapDemoPublish-Subscribe','queue.proxy.message.pubsub.emapDemoPublish-Subscribe','5','1',NULL,'routingkey.proxy.message.pubsub.emapDemoPublish-Subscribe','1','0','0','publish-subscribe','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('82','jnhiuerhskjdhfiasdkjhasdlkwerf','emapDemoRpcRequest','queue.proxy.message.rpc.emapDemoRpcRequest','7','1',NULL,'routingkey.proxy.message.rpc.emapDemoRpcRequest','1','0','0','rpcrequest','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('83','mshdfjbqwejhfgasdfbjqkygaksdfa','emapDemoRpcResponse','queue.proxy.message.rpc.emapDemoRpcResponse','7','1',NULL,'routingkey.proxy.message.rpc.emapDemoRpcResponse','1','0','0','rpcresponse','banyanAdmin','3',NULL,NULL,NULL,NULL,NULL,'AUDIT_SUCCESS',NULL,NULL,NULL,'2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('9','hkajhdfiuwxjdhakjdshuuuqoxdfasg','file','exchange.proxy.log.file','2','1',NULL,NULL,'1','1',NULL,'consume','banyanAdmin',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'); /*!40000 ALTER TABLE `NODE` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `NODE_AUDIT_HISTORY` -- DROP TABLE IF EXISTS `NODE_AUDIT_HISTORY`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `NODE_AUDIT_HISTORY` ( `NODE_AUDIT_ID` varchar(20) NOT NULL, `NODE_ID` varchar(20) DEFAULT NULL, `AUDIT_TYPE_CODE` varchar(100) DEFAULT NULL, `AUDITOR` varchar(20) DEFAULT NULL, `FROM_DATE` datetime DEFAULT NULL, `THRU_DATE` datetime DEFAULT NULL, `LAST_UPDATED_STAMP` datetime DEFAULT NULL, `LAST_UPDATED_TX_STAMP` datetime DEFAULT NULL, `CREATED_STAMP` datetime DEFAULT NULL, `CREATED_TX_STAMP` datetime DEFAULT NULL, PRIMARY KEY (`NODE_AUDIT_ID`), KEY `fk_NodeAuditHistory_AuditType` (`AUDIT_TYPE_CODE`), KEY `fk_NodeAuditHistory_Node` (`NODE_ID`), KEY `fk_NodeAuditHistory_UserLogin` (`AUDITOR`), KEY `ND_ADT_HSTR_TXSTMP` (`LAST_UPDATED_TX_STAMP`), KEY `ND_ADT_HSTR_TXCRTS` (`CREATED_TX_STAMP`), CONSTRAINT `fk_NodeAuditHistory_Node` FOREIGN KEY (`NODE_ID`) REFERENCES `NODE` (`NODE_ID`), CONSTRAINT `fk_NodeAuditHistory_AuditType` FOREIGN KEY (`AUDIT_TYPE_CODE`) REFERENCES `AUDIT_TYPE` (`AUDIT_TYPE_CODE`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `NODE_AUDIT_HISTORY` -- LOCK TABLES `NODE_AUDIT_HISTORY` WRITE; /*!40000 ALTER TABLE `NODE_AUDIT_HISTORY` DISABLE KEYS */; /*!40000 ALTER TABLE `NODE_AUDIT_HISTORY` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `QUEUE_RATE_WARNING` -- DROP TABLE IF EXISTS `QUEUE_RATE_WARNING`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `QUEUE_RATE_WARNING` ( `WARNING_ID` varchar(20) NOT NULL, `NODE_ID` varchar(20) DEFAULT NULL, `RATE_LIMIT` varchar(255) DEFAULT NULL, `REAL_RATE` varchar(255) DEFAULT NULL, `FROM_DATE` datetime DEFAULT NULL, `LAST_UPDATED_STAMP` datetime DEFAULT NULL, `LAST_UPDATED_TX_STAMP` datetime DEFAULT NULL, `CREATED_STAMP` datetime DEFAULT NULL, `CREATED_TX_STAMP` datetime DEFAULT NULL, PRIMARY KEY (`WARNING_ID`), KEY `fk_QueueRateWarning_Node` (`NODE_ID`), KEY `Q_RT_WRNNG_TXSTMP` (`LAST_UPDATED_TX_STAMP`), KEY `Q_RT_WRNNG_TXCRTS` (`CREATED_TX_STAMP`), CONSTRAINT `fk_QueueRateWarning_Node` FOREIGN KEY (`NODE_ID`) REFERENCES `NODE` (`NODE_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `QUEUE_RATE_WARNING` -- LOCK TABLES `QUEUE_RATE_WARNING` WRITE; /*!40000 ALTER TABLE `QUEUE_RATE_WARNING` DISABLE KEYS */; INSERT INTO `QUEUE_RATE_WARNING` VALUES ('1','74','2000','4000','2015-03-28 10:12:11','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('2','74','2000','4321','2015-03-28 11:02:11','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('3','74','2000','5242','2015-03-28 12:43:11','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('4','74','2000','4324','2015-03-28 13:45:11','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('5','74','2000','7645','2015-03-28 14:23:11','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'); /*!40000 ALTER TABLE `QUEUE_RATE_WARNING` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `SINK` -- DROP TABLE IF EXISTS `SINK`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `SINK` ( `SINK_ID` varchar(20) NOT NULL, `TOKEN` varchar(255) DEFAULT NULL, `FLOW_FROM` varchar(20) DEFAULT NULL, `FROM_COMMUNICATE_TYPE` varchar(100) DEFAULT NULL, `FLOW_TO` varchar(20) DEFAULT NULL, `TO_COMMUNICATE_TYPE` varchar(100) DEFAULT NULL, `ENABLE` char(1) DEFAULT NULL, `CREATOR` varchar(20) DEFAULT NULL, `AUDIT_TYPE_CODE` varchar(100) DEFAULT NULL, `LAST_UPDATED_STAMP` datetime DEFAULT NULL, `LAST_UPDATED_TX_STAMP` datetime DEFAULT NULL, `CREATED_STAMP` datetime DEFAULT NULL, `CREATED_TX_STAMP` datetime DEFAULT NULL, PRIMARY KEY (`SINK_ID`), KEY `fk_Sink_UserLogin` (`CREATOR`), KEY `fk_Sink_AuditType` (`AUDIT_TYPE_CODE`), KEY `fk_Sink_Node_flowFrom` (`FLOW_FROM`), KEY `fk_Sink_Node_flowTo` (`FLOW_TO`), KEY `SINK_TXSTMP` (`LAST_UPDATED_TX_STAMP`), KEY `SINK_TXCRTS` (`CREATED_TX_STAMP`), CONSTRAINT `fk_Sink_Node_flowTo` FOREIGN KEY (`FLOW_TO`) REFERENCES `NODE` (`NODE_ID`), CONSTRAINT `fk_Sink_AuditType` FOREIGN KEY (`AUDIT_TYPE_CODE`) REFERENCES `AUDIT_TYPE` (`AUDIT_TYPE_CODE`), CONSTRAINT `fk_Sink_Node_flowFrom` FOREIGN KEY (`FLOW_FROM`) REFERENCES `NODE` (`NODE_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `SINK` -- LOCK TABLES `SINK` WRITE; /*!40000 ALTER TABLE `SINK` DISABLE KEYS */; INSERT INTO `SINK` VALUES ('1','masdjfqiowieqooeirfajhfihfweld','12','request','10','response','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('10','nclajsdljhqiuwehfiusaiudfhiausd','49','publish','80','subscribe','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('100','iuwqijshdiuqjbuisdfiuewiuhiuiqw','100',NULL,'105',NULL,'1','banyanAdmin','AUDIT_BEFORE','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('101','akjfqkanlkjsdhfiqnlkajdhaskjasd','104',NULL,'101',NULL,'1','banyanAdmin','AUDIT_BEFORE','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('11','njafjqwhefuyasdafkbdgfkauyeqwku','79','publish','50','subscribe','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('12','migurhiquehoiusdhfouwiefhasboudy','79','publish','80','subscribe','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('13','qjenfiuqwefqiefoiusdfie2hfbwhfbd','300','produce','201','consume','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('14','kjandkjfnasdjkfasndfalkjbwejlaas','400','produce','401','consume','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('2','masuehiuiauhfiuqoquhaisudfhuqe','13','request','11','response','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17','2015-04-28 19:47:17'),('3','hlkasjdhfkqlwhlfalksjdhgssssas','43','produce','74','consume','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('4','jhlkasdfkjhasdfqwkasdfjqkwjhas','73','produce','45','produce-consume','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('5','cakjdhfjasdflqjoiajsdjflqkuwef','46','request','77','response','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('6','cnjasdfluqhwehlkjqwheaizjdhasd','78','request-response','47','response','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('7','klasehnfkljashdnflhkjahwlekdjf','52','rpcrequest','83','rpcresponse','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('8','kjashdlkfjabsdkfjhbakwjlkhqwjh','82','rpcrequest','53','rpcresponse','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'),('9','laksjdfkjiqwheiuudiuhvqweqwefa','49','publish','50','subscribe','1','banyanAdmin','AUDIT_SUCCESS','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40','2015-04-28 19:48:40'); /*!40000 ALTER TABLE `SINK` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `SINK_AUDIT_HISTORY` -- DROP TABLE IF EXISTS `SINK_AUDIT_HISTORY`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `SINK_AUDIT_HISTORY` ( `SINK_AUDIT_ID` varchar(20) NOT NULL, `SINK_ID` varchar(20) DEFAULT NULL, `AUDIT_TYPE_CODE` varchar(100) DEFAULT NULL, `AUDITOR` varchar(20) DEFAULT NULL, `FROM_DATE` datetime DEFAULT NULL, `THRU_DATE` datetime DEFAULT NULL, `LAST_UPDATED_STAMP` datetime DEFAULT NULL, `LAST_UPDATED_TX_STAMP` datetime DEFAULT NULL, `CREATED_STAMP` datetime DEFAULT NULL, `CREATED_TX_STAMP` datetime DEFAULT NULL, PRIMARY KEY (`SINK_AUDIT_ID`), KEY `fk_SinkAuditHistory_AuditType` (`AUDIT_TYPE_CODE`), KEY `fk_SinkAuditHistory_Sink` (`SINK_ID`), KEY `fk_SinkAuditHistory_UserLogin` (`AUDITOR`), KEY `SNK_ADT_HSR_TXSTMP` (`LAST_UPDATED_TX_STAMP`), KEY `SNK_ADT_HSR_TXCRTS` (`CREATED_TX_STAMP`), CONSTRAINT `fk_SinkAuditHistory_Sink` FOREIGN KEY (`SINK_ID`) REFERENCES `SINK` (`SINK_ID`), CONSTRAINT `fk_SinkAuditHistory_AuditType` FOREIGN KEY (`AUDIT_TYPE_CODE`) REFERENCES `AUDIT_TYPE` (`AUDIT_TYPE_CODE`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `SINK_AUDIT_HISTORY` -- LOCK TABLES `SINK_AUDIT_HISTORY` WRITE; /*!40000 ALTER TABLE `SINK_AUDIT_HISTORY` DISABLE KEYS */; /*!40000 ALTER TABLE `SINK_AUDIT_HISTORY` ENABLE KEYS */; UNLOCK TABLES; -- -- Dumping events for database 'banyan_DB' -- -- -- Dumping routines for database 'banyan_DB' -- /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2015-05-06 9:11:30 ================================================ FILE: pom.xml ================================================ 4.0.0 com.messagebus messagebus pom 1.0.5 client common-component interactor-component scenario httpbridge a message bus junit junit 4.11 test commons-logging commons-logging 1.1.1 log4j log4j 1.2.16 org.jetbrains annotations 13.0 com.rabbitmq amqp-client 3.5.3 com.google.code.gson gson 2.2.4 de.undercouch bson4jackson 2.4.0 dom4j dom4j 1.6.1 jaxen jaxen 1.1.6 com.google.guava guava 18.0 ================================================ FILE: scenario/pom.xml ================================================ messagebus com.messagebus 1.0.5 4.0.0 scenario com.messagebus common 1.0.5 com.messagebus client 1.0.5 ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/client/BroadcastSubscribe.java ================================================ package com.messagebus.scenario.client; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; public class BroadcastSubscribe { private static final Log logger = LogFactory.getLog(BroadcastSubscribe.class); private static final String host = "127.0.0.1"; private static final int port = 6379; public static void main(String[] args) { broadcast(); subscribe1(); subscribe2(); } private static void broadcast() { String secret = "mucasdjfaskdufhqiiuuasdfasdnus"; String token = "qiakdjfanekisdfadfhkqljwqheu"; MessagebusSinglePool singlePool = new MessagebusSinglePool(host); Messagebus client = singlePool.getResource(); Message msg = MessageFactory.createMessage(); msg.setContentType("text/plain"); msg.setContentEncoding("utf-8"); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); client.broadcast(secret, new Message[]{msg}); logger.info(" broadcast! "); singlePool.returnResource(client); singlePool.destroy(); } private static void subscribe1() { String secret = "kjhasdfhlkuqjhgaebjhasgdfabfak"; MessagebusSinglePool singlePool = new MessagebusSinglePool(host); Messagebus client = singlePool.getResource(); //notification handler client.setNotificationListener(new IMessageReceiveListener() { @Override public void onMessage(Message message) { logger.info("received notification !"); logger.info(message.getMessageId()); } }); //business handler client.consume(secret, 3, TimeUnit.SECONDS, new IMessageReceiveListener() { @Override public void onMessage(Message message) { logger.info(message.getMessageId()); } }); singlePool.returnResource(client); singlePool.destroy(); } private static void subscribe2() { String sercet = "zxdjnflakwenklasjdflkqpiasdfnj"; MessagebusSinglePool singlePool = new MessagebusSinglePool(host); Messagebus client = singlePool.getResource(); client.setNotificationListener(new IMessageReceiveListener() { @Override public void onMessage(Message message) { logger.info("received notification !"); logger.info(message.getMessageId()); } }); client.consume(sercet, 3, TimeUnit.SECONDS, new IMessageReceiveListener() { @Override public void onMessage(Message message) { logger.info(message.getMessageId()); } }); singlePool.returnResource(client); singlePool.destroy(); } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/client/ClientUnderMultiThread.java ================================================ package com.messagebus.scenario.client; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 3/6/15. */ public class ClientUnderMultiThread { private static final Log logger = LogFactory.getLog(ClientUnderMultiThread.class); private static final String pubsuberHost = "127.0.0.1"; private static final int pubsuberPort = 6379; public static void main(String[] args) { logger.info("start produce..."); ProduceClient produceClient = new ProduceClient(); produceClient.simulate(); try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("start consume..."); ConsumeClient consumeClient = new ConsumeClient(); consumeClient.simulate(); } private static class ProduceClient { private CountDownLatch counter = new CountDownLatch(2); public void simulate() { logger.info("ProduceClient simulate start"); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(2); MessagebusSinglePool pool = new MessagebusSinglePool(pubsuberHost); Thread tmpThread; for (int i = 0; i < 2; i++) { tmpThread = new Thread(new ProduceThread(pool, counter)); tmpThread.setName("produceThread-" + i); tmpThread.start(); } try { counter.await(); } catch (InterruptedException e) { e.printStackTrace(); } pool.destroy(); logger.info("ProduceClient simulate end"); } } private static class ConsumeClient { private CountDownLatch counter = new CountDownLatch(2); public void simulate() { logger.info("ConsumeClient simulate start"); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(2); String appId = "D0fW8u2u1v7S1IvI8qoQg3dUlLL5b36q"; MessagebusSinglePool pool = new MessagebusSinglePool(pubsuberHost); Thread tmpThread; for (int i = 0; i < 2; i++) { tmpThread = new Thread(new ConsumeThread(pool, counter)); tmpThread.setName("consumeThread-" + i); tmpThread.start(); } try { counter.await(); } catch (InterruptedException e) { e.printStackTrace(); } pool.destroy(); logger.info("ConsumeClient simulate end"); } } private static class ProduceThread implements Runnable { private MessagebusSinglePool pool; private CountDownLatch counter; public ProduceThread(MessagebusSinglePool pool, CountDownLatch counter) { this.pool = pool; this.counter = counter; } @Override public void run() { Messagebus client = pool.getResource(); Message msg = MessageFactory.createMessage(); msg.setContentType("text/plain"); msg.setContentEncoding("utf-8"); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); String secret = "kljasdoifqoikjhhhqwhebasdfasdf"; String token = "hlkasjdhfkqlwhlfalksjdhgssssas"; client.produce(secret, "emapDemoConsume", msg, token); pool.returnResource(client); counter.countDown(); } } private static class ConsumeThread implements Runnable { private MessagebusSinglePool pool; private CountDownLatch counter; public ConsumeThread(MessagebusSinglePool pool, CountDownLatch counter) { this.pool = pool; this.counter = counter; } @Override public void run() { String secret = "zxdjnflakwenklasjdflkqpiasdfnj"; Messagebus client = pool.getResource(); List msgs = client.consume(secret, 10); pool.returnResource(client); for (Message msg : msgs) { logger.info(msg.getMessageId()); } counter.countDown(); } } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/client/ProduceConsume.java ================================================ package com.messagebus.scenario.client; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import com.messagebus.scenario.util.PropertiesHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.List; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 2/23/15. */ public class ProduceConsume { private static final Log logger = LogFactory.getLog(ProduceConsume.class); public static void main(String[] args) { produce(); consumeWithPushStyle(); //or // consumeWithPullStyle(); //or async consume // asyncConsume(); } private static void produce() { String secret = "kljasdoifqoikjhhhqwhebasdfasdf"; String token = "hlkasjdhfkqlwhlfalksjdhgssssas"; MessagebusSinglePool singlePool = new MessagebusSinglePool( PropertiesHelper.getPropertyValue("messagebus.pubsuber.host") ); Messagebus client = singlePool.getResource(); Message msg = MessageFactory.createMessage(); msg.setContentType("text/plain"); msg.setContentEncoding("utf-8"); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); client.produce(secret, "emapDemoConsume", msg, token); singlePool.returnResource(client); singlePool.destroy(); } private static void consumeWithPullStyle() { String secret = "zxdjnflakwenklasjdflkqpiasdfnj"; MessagebusSinglePool singlePool = new MessagebusSinglePool( PropertiesHelper.getPropertyValue("messagebus.pubsuber.host") ); Messagebus client = singlePool.getResource(); List msgs = client.consume(secret, 1); for (Message msg : msgs) { logger.info(msg.getMessageId()); } singlePool.returnResource(client); singlePool.destroy(); } private static void consumeWithPushStyle() { String secret = "zxdjnflakwenklasjdflkqpiasdfnj"; MessagebusSinglePool singlePool = new MessagebusSinglePool( PropertiesHelper.getPropertyValue("messagebus.pubsuber.host") ); Messagebus client = singlePool.getResource(); client.consume(secret, 10, TimeUnit.SECONDS, new IMessageReceiveListener() { @Override public void onMessage(Message message) { logger.info(message.getMessageId()); } }); singlePool.returnResource(client); singlePool.destroy(); } private static void asyncConsume() { AsyncConsumeThread asyncConsumeThread = new AsyncConsumeThread(); asyncConsumeThread.startup(); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } asyncConsumeThread.shutdown(); } private static class AsyncConsumeThread implements Runnable { private Thread currentThread; private MessagebusSinglePool singlePool; private Messagebus client; public AsyncConsumeThread() { this.currentThread = new Thread(this); this.currentThread.setName("AsyncConsumeThread"); this.currentThread.setDaemon(true); } @Override public void run() { String secret = "zxdjnflakwenklasjdflkqpiasdfnj"; singlePool = new MessagebusSinglePool( PropertiesHelper.getPropertyValue("messagebus.pubsuber.host") ); client = singlePool.getResource(); //register notification listener client.setNotificationListener(new IMessageReceiveListener() { @Override public void onMessage(Message message) { logger.info("received notification : " + message.getAppId()); } }); //long long time client.consume(secret, Integer.MAX_VALUE, TimeUnit.SECONDS, new IMessageReceiveListener() { @Override public void onMessage(Message message) { logger.info(message.getMessageId()); } }); } public void startup() { this.currentThread.start(); } public void shutdown() { singlePool.returnResource(client); singlePool.destroy(); this.currentThread.interrupt(); } } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/client/PublishSubscribe.java ================================================ package com.messagebus.scenario.client; import com.messagebus.client.IMessageReceiveListener; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 2/24/15. */ public class PublishSubscribe { private static final Log logger = LogFactory.getLog(PublishSubscribe.class); private static final String host = "127.0.0.1"; private static final int port = 6379; public static void main(String[] args) { publish(); subscribe1(); subscribe2(); } private static void publish() { String secret = "oiqwenncuicnsdfuasdfnkajkwqowe"; MessagebusSinglePool singlePool = new MessagebusSinglePool(host); Messagebus client = singlePool.getResource(); Message msg = MessageFactory.createMessage(); msg.setContentType("text/plain"); msg.setContentEncoding("utf-8"); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); client.publish(secret, new Message[]{msg}); singlePool.returnResource(client); singlePool.destroy(); } private static void subscribe1() { String secret = "nckljsenlkjanefluiwnlanfmsdfas"; String token = "kjkjasdjfhkajsdfhksdjhfkasdf"; MessagebusSinglePool singlePool = new MessagebusSinglePool(host); Messagebus client = singlePool.getResource(); client.subscribe(secret, "", token, new IMessageReceiveListener() { @Override public void onMessage(Message message) { logger.info(message.getMessageId()); } }, 3, TimeUnit.SECONDS); singlePool.returnResource(client); singlePool.destroy(); } private static void subscribe2() { String secret = "zxcnvblawelkusahdfqwiuhowefhnx"; String token = "kjkjasdjfhkajsdfhksdjhfkasdf"; MessagebusSinglePool singlePool = new MessagebusSinglePool(host); Messagebus client = singlePool.getResource(); client.subscribe(secret, "", token, new IMessageReceiveListener() { @Override public void onMessage(Message message) { logger.info(message.getMessageId()); } }, 3, TimeUnit.SECONDS); singlePool.returnResource(client); singlePool.destroy(); } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/client/RequestResponse.java ================================================ package com.messagebus.scenario.client; import com.google.common.base.Strings; import com.messagebus.client.IRequestListener; import com.messagebus.client.MessageResponseTimeoutException; import com.messagebus.client.Messagebus; import com.messagebus.client.MessagebusSinglePool; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.concurrent.TimeUnit; /** * Created by yanghua on 2/24/15. */ public class RequestResponse { private static final Log logger = LogFactory.getLog(RequestResponse.class); private static final String host = "127.0.0.1"; private static final int port = 6379; public static void main(String[] args) { response(); request(); } private static void request() { String secret = "iuoqiwejicaoisfaisfbsqewnfjnfa"; String token = "cakjdhfjasdflqjoiajsdjflqkuwef"; MessagebusSinglePool singlePool = new MessagebusSinglePool(host); Messagebus client = singlePool.getResource(); Message msg = MessageFactory.createMessage(); msg.setContentType("text/plain"); msg.setContentEncoding("utf-8"); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); Message responseMsg = null; try { responseMsg = client.request(secret, "emapDemoResponse", msg, token, 10); } catch (MessageResponseTimeoutException e) { e.printStackTrace(); } singlePool.returnResource(client); singlePool.destroy(); if (responseMsg != null) { logger.info("received response message : " + responseMsg.getCorrelationId()); } } private static void response() { new Thread(new Runnable() { @Override public void run() { final String secret = "zxjhvclawenlkfhsladfnqpwenflak"; MessagebusSinglePool singlePool = new MessagebusSinglePool(host); final Messagebus client = singlePool.getResource(); client.response(secret, new IRequestListener() { @Override public Message onRequest(Message requestMsg) { if (Strings.isNullOrEmpty(requestMsg.getCorrelationId())) { logger.info("got requested message : " + requestMsg.getCorrelationId()); } Message respMsg = MessageFactory.createMessage(); respMsg.setContentType("text/plain"); respMsg.setContentEncoding("utf-8"); respMsg.setCorrelationId(requestMsg.getCorrelationId()); respMsg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); return respMsg; } }, 10, TimeUnit.SECONDS); singlePool.returnResource(client); singlePool.destroy(); } }).start(); } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/client/XSLTTestCase.java ================================================ package com.messagebus.scenario.client; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; /** * Created by yanghua on 4/4/15. */ public class XSLTTestCase { public static void main(String[] args) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Source xmlSource = new StreamSource(classLoader.getResourceAsStream("producerXml.xml")); Source xsltSource = new StreamSource(classLoader.getResourceAsStream("pToc.xsl")); TransformerFactory transFact = TransformerFactory.newInstance(); try { Transformer trans = transFact.newTransformer(xsltSource); trans.transform(xmlSource, new StreamResult(System.out)); } catch (TransformerException e) { e.printStackTrace(); } } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/httpBridge/ProduceConsume.java ================================================ package com.messagebus.scenario.httpBridge; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.client.message.model.MessageJSONSerializer; import com.messagebus.common.Constants; import com.messagebus.scenario.util.PropertiesHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ProduceConsume { private static final Log logger = LogFactory.getLog(ProduceConsume.class); public static void main(String[] args) { testProduceWithPost(); // testProduceWithGet(); testConsume("pull"); } private static void testProduceWithPost() { String testUrlFormat = "http://%s:%s/messagebus/queues/%s/messages?secret=%s&apiType=produce&token=%s"; String testQueue = "appDataQueue"; String secret = "iojawdnaisdflknoiankjfdblaidcas"; String token = "iojawdnaisdflknoiankjfdblaidcas"; String url = String.format(testUrlFormat, PropertiesHelper.getPropertyValue("messagebus.httpbridge.host"), Integer.parseInt(PropertiesHelper.getPropertyValue("messagebus.httpbridge.port")), testQueue, secret, token); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = null; Message msg = MessageFactory.createMessage(); msg.setReplyTo(testQueue); msg.setContentType("text/plain"); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); List msgs = new ArrayList(1); msgs.add(msg); String msgs2json = MessageJSONSerializer.serializeMessages(msgs); try { HttpPost postRequest = new HttpPost(url); List nvps = new ArrayList(); nvps.add(new BasicNameValuePair("messages", msgs2json)); postRequest.setEntity(new UrlEncodedFormEntity(nvps)); response = httpClient.execute(postRequest); HttpEntity entity = response.getEntity(); if (entity != null) { logger.info("response is : " + EntityUtils.toString(entity)); } } catch (IOException e) { logger.error("[syncHTTPGet] occurs a IOException : " + e.getMessage()); } } private static void testProduceWithGet() { String testUrlFormat = "http://%s:%s/messagebus/queues/%s/messages?secret=%s&apiType=produce&token=%s"; String testQueue = "appDataQueue"; String secret = "iojawdnaisdflknoiankjfdblaidcas"; String token = "iojawdnaisdflknoiankjfdblaidcas"; String url = String.format(testUrlFormat, PropertiesHelper.getPropertyValue("messagebus.httpbridge.host"), Integer.parseInt(PropertiesHelper.getPropertyValue("messagebus.httpbridge.port")), testQueue, secret, token); url += "&content=text&contentType=text/plain"; HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = null; try { response = httpClient.execute(new HttpGet(url)); HttpEntity entity = response.getEntity(); if (entity != null) { logger.info("response is : " + EntityUtils.toString(entity)); } } catch (IOException e) { logger.error("[syncHTTPGet] occurs a IOException : " + e.getMessage()); } } private static void testConsume(String mode) { String testUrlFormat = "http://%s:%s/messagebus/queues/messages?secret=%s&apiType=consume&mode=%s&num=1"; String secret = "iojawdnaisdflknoiankjfdblaidcas"; String url = String.format(testUrlFormat, PropertiesHelper.getPropertyValue("messagebus.httpbridge.host"), Integer.parseInt(PropertiesHelper.getPropertyValue("messagebus.httpbridge.port")), secret, mode); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = null; try { response = httpClient.execute(new HttpGet(url)); HttpEntity entity = response.getEntity(); if (entity != null) { logger.info("response is : " + EntityUtils.toString(entity)); } } catch (IOException e) { logger.error("[syncHTTPGet] occurs a IOException : " + e.getMessage()); } } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/httpBridge/PublishSubscribe.java ================================================ package com.messagebus.scenario.httpBridge; import com.google.gson.Gson; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import com.messagebus.client.message.model.MessageJSONSerializer; import com.messagebus.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by yanghua on 3/19/15. */ public class PublishSubscribe { private static final Log logger = LogFactory.getLog(PublishSubscribe.class); private static final Gson gson = new Gson(); private static String testHost = "localhost"; private static int testPort = 8080; public static void main(String[] args) { testPublish(); testSubscribe(); } private static void testPublish() { String testUrlFormat = "http://%s:%s/messagebus/queues/messages?secret=%s&type=publish&token=%s"; String secret = "oiqwenncuicnsdfuasdfnkajkwqowe"; String token = "kjkjasdjfhkajsdfhksdjhfkasdf"; String url = String.format(testUrlFormat, testHost, testPort, secret, token); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = null; Message msg = MessageFactory.createMessage(); msg.setContent("test".getBytes(Constants.CHARSET_OF_UTF8)); List msgs = new ArrayList(1); msgs.add(msg); String msgs2json = MessageJSONSerializer.serializeMessages(msgs); try { HttpPost postRequest = new HttpPost(url); List nvps = new ArrayList(); nvps.add(new BasicNameValuePair("messages", msgs2json)); postRequest.setEntity(new UrlEncodedFormEntity(nvps)); response = httpClient.execute(postRequest); HttpEntity entity = response.getEntity(); if (entity != null) { logger.info("response is : " + EntityUtils.toString(entity)); } } catch (IOException e) { logger.error("[syncHTTPGet] occurs a IOException : " + e.getMessage()); } } private static void testSubscribe() { String testUrlFormat = "http://%s:%s/messagebus/queues/messages?secret=%s&type=subscribe"; String secret = "nckljsenlkjanefluiwnlanfmsdfas"; String url = String.format(testUrlFormat, testHost, testPort, secret); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = null; try { response = httpClient.execute(new HttpGet(url)); HttpEntity entity = response.getEntity(); if (entity != null) { logger.info("response is : " + EntityUtils.toString(entity)); } } catch (IOException e) { logger.error("[syncHTTPGet] occurs a IOException : " + e.getMessage()); } } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/httpBridge/RequestTemplate.java ================================================ package com.messagebus.scenario.httpBridge; import com.messagebus.client.message.model.Message; import com.messagebus.client.message.model.MessageFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.DefaultHttpClient; public class RequestTemplate { private static final Log logger = LogFactory.getLog(RequestTemplate.class); private static String testUrlFormat = "http://%s:%s/messagebus/queues/%s/messages?appkey=%s&type=request&timeout=%s"; private static String testHost = "localhost"; private static int testPort = 8081; private static String testQueue = "crm"; private static String appkey = "jahksjdfhakjdflkasdjflk"; private static long timeout = 5000; public static void main(String[] args) { String url = String.format(testUrlFormat, testHost, testPort, testQueue, appkey, timeout); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = null; Message msg = MessageFactory.createMessage(); msg.setReplyTo(testQueue); msg.setContent("test".getBytes()); // String msg2json = MessageJSONSerializer.serialize(testMsg); // // try { // HttpPost postRequest = new HttpPost(url); // List nvps = new ArrayList<>(); // nvps.add(new BasicNameValuePair("message", msg2json)); // postRequest.setEntity(new UrlEncodedFormEntity(nvps)); // // response = httpClient.execute(postRequest); // HttpEntity entity = response.getEntity(); // if (entity != null) { // long len = entity.getContentLength(); // if (len == -1) // logger.error("there is no response data."); // else if (len < 2 * 1024 * 1024) { // logger.info("response is : " + EntityUtils.toString(entity)); // } else { // logger.error("[syncHTTPGet] response length is too large : (" + len + ") B " + // "; and the url is : " + url); // } // } // } catch (IOException e) { // logger.error("[syncHTTPGet] occurs a IOException : " + e.getMessage()); // } finally { // if (response != null) // try { // response.close(); // } catch (IOException e) { // logger.error("[syncHTTPGet] finally block occurs a IOException : " + e.getMessage()); // } // } } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/httpBridge/ResponseTemplate.java ================================================ package com.messagebus.scenario.httpBridge; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ResponseTemplate { //there are two end points, follow there steps: /* send a request (end point 1) get a response (end point 2) send a response (end point 2) get a response (end point 1) */ private static final Log logger = LogFactory.getLog(ResponseTemplate.class); private static String testUrlFormat = "http://%s:%s/messagebus/queues/%s/messages?appkey=%s&type=%s&timeout=%s"; private static String testHost = "localhost"; private static int testPort = 8081; private static String testQueue = "crm"; private static String appkey = "jahksjdfhakjdflkasdjflk"; private static long timeout = 30000; private static volatile Object lockObj = new Object(); public static void main(String[] args) { // try { // EndPoint1 e1 = new EndPoint1(); // e1.start(); // // TimeUnit.SECONDS.sleep(3); // // EndPoint2 e2 = new EndPoint2(); // e2.start(); // // //block 30s // TimeUnit.SECONDS.sleep(40); // } catch (InterruptedException e) { // // } } // private static class EndPoint1 extends Thread { // // @Override // public void run() { // CloseableHttpResponse response = null; // try { // String url = String.format(testUrlFormat, testHost, testPort, testQueue, appkey, "request", timeout); // // CloseableHttpClient httpClient = HttpClients.createDefault(); // // Message testMsg = MessageFactory.createMessage(MessageType.QueueMessage); // testMsg.setContent("test".getBytes()); // // String msg2json = MessageJSONSerializer.serialize(testMsg); // // HttpPost postRequest = new HttpPost(url); // List nvps = new ArrayList(); // nvps.add(new BasicNameValuePair("message", msg2json)); // postRequest.setEntity(new UrlEncodedFormEntity(nvps)); // // response = httpClient.execute(postRequest); // // HttpEntity entity = response.getEntity(); // if (entity != null) { // logger.info("end point 1 received response : " + EntityUtils.toString(entity)); // } // } catch (IOException e) { // logger.error("[syncHTTPGet] occurs a IOException : " + e.getMessage()); // } finally { // if (response != null) // try { // response.close(); // } catch (IOException e) { // logger.error("[syncHTTPGet] finally block occurs a IOException : " + e.getMessage()); // } // } // } // } // // private static class EndPoint2 extends Thread { // // @Override // public void run() { // CloseableHttpResponse response = null; // CloseableHttpResponse resp = null; // try { // synchronized (lockObj) { // String url = String.format(testUrlFormat, testHost, testPort, testQueue, appkey, "consume", timeout); // url += "&mode=sync&num=1"; // // CloseableHttpClient httpClient = HttpClients.createDefault(); // HttpGet get = new HttpGet(url); // // //get request // response = httpClient.execute(get); // HttpEntity entity = response.getEntity(); // if (entity != null) { // String responseData = EntityUtils.toString(entity); // logger.info("end point 2 : received response : " + responseData); // // Message msg = extractRequestMsg(responseData); // // String tmpQueueName = String.valueOf(msg.getMessageId()); // // String responseUrl = String.format(testUrlFormat, testHost, testPort, // tmpQueueName, appkey, "response", timeout); // // //send response // CloseableHttpClient responseHttpClient = HttpClients.createDefault(); // // // Message testMsg = MessageFactory.createMessage(MessageType.QueueMessage); // testMsg.setContent("test".getBytes()); // // String msg2json = MessageJSONSerializer.serialize(testMsg); // // HttpPost postRequest = new HttpPost(responseUrl); // List nvps = new ArrayList(); // nvps.add(new BasicNameValuePair("message", msg2json)); // postRequest.setEntity(new UrlEncodedFormEntity(nvps)); // // resp = responseHttpClient.execute(postRequest); // HttpEntity responseEntity = response.getEntity(); // // lockObj.notify(); // } // } // } catch (IOException e) { // logger.error("[syncHTTPGet] occurs a IOException : " + e.getMessage()); // } finally { // if (response != null) // try { // response.close(); // } catch (IOException e) { // logger.error("[syncHTTPGet] finally block occurs a IOException : " + e.getMessage()); // } // // if (resp != null) // try { // resp.close(); // } catch (IOException e) { // logger.error("[syncHTTPGet] finally block occurs a IOException : " + e.getMessage()); // } // } // } // } // // private static Message extractRequestMsg(String respData) { // JsonParser parser = new JsonParser(); // JsonElement element = parser.parse(respData); // JsonObject object = element.getAsJsonObject(); // JsonElement dataElement = object.get("data"); // if (!dataElement.isJsonArray()) { // return null; // } // // JsonElement msgElement = dataElement.getAsJsonArray().get(0); // // return MessageJSONSerializer.deSerialize(msgElement, MessageType.QueueMessage); // } } ================================================ FILE: scenario/src/main/java/com/messagebus/scenario/util/PropertiesHelper.java ================================================ package com.messagebus.scenario.util; import java.io.IOException; import java.util.Properties; /** * Created by yanghua on 3/25/15. */ public class PropertiesHelper { private static final Properties commonProperties; static { commonProperties = new Properties(); try { commonProperties.load(PropertiesHelper.class.getClassLoader().getResourceAsStream("common.properties")); } catch (IOException e) { e.printStackTrace(); } } public static String getPropertyValue(String key) { return commonProperties.getProperty(key); } } ================================================ FILE: scenario/src/main/resources/common.properties ================================================ messagebus.pubsuber.host=172.16.206.250 messagebus.pubsuber.port=6379 messagebus.httpbridge.host=172.16.206.28 messagebus.httpbridge.port=8081 ================================================ FILE: scenario/src/main/resources/consumerSchema.xml ================================================ ================================================ FILE: scenario/src/main/resources/pToc.xsl ================================================ 1 ================================================ FILE: scenario/src/main/resources/producerSchema.xml ================================================ ================================================ FILE: scenario/src/main/resources/producerXml.xml ================================================ A07021012 jack 1 1986/10/20 Computer Since and technology