Java SDK

WeEvent支持RESTfulJsonRPCSTOMPMQTT等协议,方便各种语言的接入和适配。

同时为Java语言提供了独立的SDK。其他语言SDK在规划中,欢迎大家贡献代码,WeEvent代码仓库

集成SDK

  • gradle依赖
  1. implement 'com.webank.weevent:weevent-client:1.0.0'
  • maven依赖
  1. <dependency>
  2. <groupId>com.webank.weevent</groupId>
  3. <artifactId>weevent-client</artifactId>
  4. <version>1.0.0</version>
  5. </dependency>

API接口

  1. public interface IWeEventClient {
  2. /**
  3. * Get the client handler of WeEvent's broker with default url, http://localhost:8080/weevent.
  4. *
  5. * @throws BrokerException broker exception
  6. */
  7. static IWeEventClient build() throws BrokerException {
  8. return new WeEventClient();
  9. }
  10.  
  11. /**
  12. * Get the client handler of WeEvent's broker with custom url.
  13. *
  14. * @param brokerUrl WeEvent's broker url, like http://localhost:8080/weevent
  15. * @throws BrokerException broker exception
  16. */
  17. static IWeEventClient build(String brokerUrl) throws BrokerException {
  18. return new WeEventClient(brokerUrl);
  19. }
  20.  
  21. /**
  22. * Get the client handler of WeEvent's broker custom url and account authorization.
  23. *
  24. * @param brokerUrl WeEvent's broker url, like http://localhost:8080/weevent
  25. * @param userName account name
  26. * @param password password
  27. * @throws BrokerException broker exception
  28. */
  29. static IWeEventClient build(String brokerUrl, String userName, String password) throws BrokerException {
  30. return new WeEventClient(brokerUrl, userName, password);
  31. }
  32.  
  33. /**
  34. * Publish an event to topic.
  35. *
  36. * @param topic topic name
  37. * @param content topic data
  38. * @return send result, SendResult.SUCCESS if success, and SendResult.eventId
  39. * @throws BrokerException broker exception
  40. */
  41. SendResult publish(String topic, byte[] content) throws BrokerException;
  42.  
  43. /**
  44. * Subscribe events from topic.
  45. *
  46. * @param topic topic name
  47. * @param offset, from next event after this offset(an event id), WeEvent.OFFSET_FIRST if from head of queue, WeEvent.OFFSET_LAST if from tail of queue
  48. * @param listener callback
  49. * @return subscription Id
  50. * @throws BrokerException invalid input param
  51. */
  52. String subscribe(String topic, String offset, WeEventClient.EventListener listener) throws BrokerException;
  53.  
  54. /**
  55. * Unsubscribe an exist subscription subscribed by subscribe interface.
  56. * The consumer will no longer receive messages from broker after this.
  57. *
  58. * @param subscriptionId invalid input
  59. * @return success if true
  60. * @throws BrokerException broker exception
  61. */
  62.  
  63. boolean unSubscribe(String subscriptionId) throws BrokerException;
  64.  
  65. /**
  66. * Open a topic
  67. *
  68. * @param topic topic name
  69. * @return true if success
  70. * @throws BrokerException broker exception
  71. */
  72. boolean open(String topic) throws BrokerException;
  73.  
  74. /**
  75. * Close a topic.
  76. *
  77. * @param topic topic name
  78. * @return true if success
  79. * @throws BrokerException broker exception
  80. */
  81. boolean close(String topic) throws BrokerException;
  82.  
  83. /**
  84. * Check a topic is exist or not.
  85. *
  86. * @param topic topic name
  87. * @return true if exist
  88. * @throws BrokerException broker exception
  89. */
  90. boolean exist(String topic) throws BrokerException;
  91.  
  92. /**
  93. * List all topics in WeEvent's broker.
  94. *
  95. * @param pageIndex page index, from 0
  96. * @param pageSize page size, [10, 100)
  97. * @return topic list
  98. * @throws BrokerException broker exception
  99. */
  100. TopicPage list(Integer pageIndex, Integer pageSize) throws BrokerException;
  101.  
  102. /**
  103. * Get a topic information.
  104. *
  105. * @param topic topic name
  106. * @return topic information
  107. * @throws BrokerException broker exception
  108. */
  109. TopicInfo state(String topic) throws BrokerException;
  110.  
  111. /**
  112. * Get an event information.
  113. *
  114. * @param eventId event id
  115. * @return WeEvent
  116. * @throws BrokerException broker exception
  117. */
  118. WeEvent getEvent(String eventId) throws BrokerException;
  119.  
  120. /**
  121. * Publish an event to topic.
  122. *
  123. * @param topic topic name
  124. * @param content topic data
  125. * @return send result, SendResult.SUCCESS if success, and SendResult.eventId
  126. * @throws BrokerException broker exception
  127. */
  128. SendResult publish(String topic, String groupId, byte[] content, Map<String, String> extensions) throws BrokerException;
  129.  
  130. /**
  131. * Subscribe events from topic.
  132. *
  133. * @param topic topic name
  134. * @param offset, from next event after this offset(an event id), WeEvent.OFFSET_FIRST if from head of queue, WeEvent.OFFSET_LAST if from tail of queue
  135. * @param listener callback
  136. * @return subscription Id
  137. * @throws BrokerException invalid input param
  138. */
  139. String subscribe(String topic, String groupId, String offset, WeEventClient.EventListener listener) throws BrokerException;
  140.  
  141. /**
  142. * Subscribe events from topic.
  143. *
  144. * @param topic topic name
  145. * @param offset, from next event after this offset(an event id), WeEvent.OFFSET_FIRST if from head of queue, WeEvent.OFFSET_LAST if from tail of queue
  146. * @param listener callback
  147. * @return subscription Id
  148. * @throws BrokerException invalid input param
  149. */
  150. String subscribe(String topic, String groupId, String offset,String contnueSubScriptionId, WeEventClient.EventListener listener) throws BrokerException;
  151.  
  152. /**
  153. * Publish an event to topic.
  154. *
  155. * @param topic topic name
  156. * @param content topic data
  157. * @return send result, SendResult.SUCCESS if success, and SendResult.eventId
  158. * @throws BrokerException broker exception
  159. */
  160. SendResult publish(String topic, byte[] content, Map<String, String> extensions) throws BrokerException;
  161.  
  162. /**
  163. * Close a topic.
  164. *
  165. * @param topic topic name
  166. * @param groupId which group to close
  167. * @return true if success
  168. * @throws BrokerException broker exception
  169. */
  170. boolean close(String topic, String groupId) throws BrokerException;
  171.  
  172. /**
  173. * Check a topic is exist or not.
  174. *
  175. * @param topic topic name
  176. * @param groupId which group to exit
  177. * @return true if exist
  178. * @throws BrokerException broker exception
  179. */
  180. boolean exist(String topic, String groupId) throws BrokerException;
  181.  
  182. /**
  183. * Open a topic.
  184. *
  185. * @param topic topic name
  186. * @param groupId which group to open
  187. * @return true if success
  188. * @throws BrokerException broker exception
  189. */
  190. boolean open(String topic, String groupId) throws BrokerException;
  191.  
  192. /**
  193. * List all topics in WeEvent's broker.
  194. *
  195. * @param pageIndex page index, from 0
  196. * @param pageSize page size, [10, 100)
  197. * @return topic list
  198. * @throws BrokerException broker exception
  199. */
  200. TopicPage list(Integer pageIndex, Integer pageSize, String groupId) throws BrokerException;
  201.  
  202. /**
  203. * Get a topic information.
  204. *
  205. * @param topic topic name
  206. * @return topic information
  207. * @throws BrokerException broker exception
  208. */
  209. TopicInfo state(String topic, String groupId) throws BrokerException;
  210.  
  211. /**
  212. * Get an event information.
  213. *
  214. * @param eventId event id
  215. * @return WeEvent
  216. * @throws BrokerException broker exception
  217. */
  218. WeEvent getEvent(String eventId, String groupId) throws BrokerException;
  219.  
  220. }

代码样例

  • WeEvent 1.0.0 版本
  1. public static void main(String[] args) {
  2. try {
  3. String url = "http://localhost:8080/weevent";
  4. IWeEventClient client = IWeEventClient.build(url);
  5.  
  6. String groupId = "1";
  7. String topicName = "com.weevent.test";
  8. // open 一个"com.weevent.test"的主题
  9. client.open(topicName,groupId);
  10.  
  11. // 用户自定义拓展必须以weevent-开头,可选参数。
  12. Map<String, String> extensions = mew HashMap<>();
  13. extensions.put("weevent-format", "json");
  14. // publish接口的参数分别是主题Topic、群组Id、事件内容Content、扩展字段
  15. client.publish(topicName, groupId, "{\"hello\":\" wolrd\"}".getBytes(), extensions);
  16. System.out.println(sendResult);
  17. } catch (BrokerException e) {
  18. e.printStackTrace();
  19. }
  20. }
  • WeEvent 1.0.0 版本样例演示如何通过Java SDK发布事件,完整的代码请参见Java SDK代码样例