03 storm 源码阅读 storm的进程间消息通信实现clojure端 加载java端netty能力

<pre name="code" class="java">;; Licensed to the Apache Software Foundation (ASF) under one
;; or more contributor license agreements.  See the NOTICE file
;; distributed with this work for additional information
;; regarding copyright ownership.  The ASF licenses this file
;; to you 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.
;;
;;主要负责调用java端的进程间通信的接口,包了一层,这样就可以通过它实现在clojure主逻辑做进程间通信。(后期版本是netty实现)
;;
;;mk-context 调用 LocalContext,LocalContext调用 LocalConnection ,LocalConnection 调用add-queue 
;;对外暴露一个  mk-context函数,返回LocalContext。 就可以使用进程间通信。
(ns backtype.storm.messaging.local
  (:refer-clojure :exclude [send])
  (:use [backtype.storm log])
  (:import [backtype.storm.messaging IContext IConnection TaskMessage])
  (:import [java.util.concurrent LinkedBlockingQueue])
  (:import [java.util Map Iterator])
  (:import [java.util Iterator ArrayList])
  (:gen-class))

;;这是有个方法。添加队列的。辅助方法
(defn add-queue! [queues-map lock storm-id port]
  (let [id (str storm-id "-" port)]
    (locking lock
      (when-not (contains? @queues-map id)
        (swap! queues-map assoc id (LinkedBlockingQueue.))))
    (@queues-map id)))

;;这个是java那边netty映射到clojure端的conection
(deftype LocalConnection [storm-id port queues-map lock queue]
  IConnection
  (^Iterator recv [this ^int flags ^int clientId]
    (when-not queue
      (throw (IllegalArgumentException. "Cannot receive on this socket")))
    (let [ret (ArrayList.)
          msg (if (= flags 1) (.poll queue) (.take queue))]
      (if msg
        (do 
          (.add ret msg)
          (.iterator ret))
        nil)))
  (^void send [this ^int taskId ^bytes payload]
    (let [send-queue (add-queue! queues-map lock storm-id port)]
      (.put send-queue (TaskMessage. taskId payload))
      ))
  (^void send [this ^Iterator iter]
    (let [send-queue (add-queue! queues-map lock storm-id port)]
      (while (.hasNext iter) 
         (.put send-queue (.next iter)))
      ))
  (^void close [this]
    ))


;;这个是java那边netty映射到clojure端的Context,里面持有LocalConnection
(deftype LocalContext [^{:unsynchronized-mutable true} queues-map
                       ^{:unsynchronized-mutable true} lock]
  IContext
  (^void prepare [this ^Map storm-conf]
    (set! queues-map (atom {}))
    (set! lock (Object.)))
  (^IConnection bind [this ^String storm-id ^int port]
    (LocalConnection. storm-id port queues-map lock (add-queue! queues-map lock storm-id port)))
  (^IConnection connect [this ^String storm-id ^String host ^int port]
    (LocalConnection. storm-id port queues-map lock nil))
  (^void term [this]
    ))

;;这个返回LocalContext,给主流程做消息传递逻辑
(defn mk-context [] 
  (let [context  (LocalContext. nil nil)]
    (.prepare ^IContext context nil)
    context))



                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值