Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
546 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/com/gojek/rabbitmq/channel_pool/RabbitMQChannelFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package gojek.rabbitmq.channel_pool; | ||
|
||
import com.rabbitmq.client.Channel; | ||
import com.rabbitmq.client.Connection; | ||
import org.apache.commons.pool2.BasePooledObjectFactory; | ||
import org.apache.commons.pool2.DestroyMode; | ||
import org.apache.commons.pool2.PooledObject; | ||
import org.apache.commons.pool2.impl.DefaultPooledObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class RabbitMQChannelFactory extends BasePooledObjectFactory<Channel> { | ||
Logger log = LoggerFactory.getLogger(this.getClass()); | ||
|
||
private final Connection connection; | ||
|
||
public RabbitMQChannelFactory(Connection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
@Override | ||
public Channel create() throws Exception { | ||
Channel channel = connection.createChannel(); | ||
log.info("Created a new channel with id: " + channel.getChannelNumber()); | ||
return channel; | ||
} | ||
|
||
|
||
@Override | ||
public boolean validateObject(PooledObject<Channel> p) { | ||
boolean open = p.getObject().isOpen(); | ||
if (!open) { | ||
log.info("Channel is closed, invalidating channel with id " + p.getObject().getChannelNumber()); | ||
} | ||
return open; | ||
} | ||
|
||
@Override | ||
public PooledObject<Channel> wrap(Channel obj) { | ||
return new DefaultPooledObject<>(obj); | ||
} | ||
|
||
|
||
@Override | ||
public void destroyObject(PooledObject<Channel> p, DestroyMode destroyMode) throws Exception { | ||
super.destroyObject(p, destroyMode); | ||
log.info("Closing the channel with id: " + p.getObject().getChannelNumber()); | ||
p.getObject().close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
(ns ziggurat.messaging.channel_pool | ||
(:require [mount.core :refer [defstate]] | ||
[ziggurat.config :as zc] | ||
[ziggurat.messaging.connection :as c] | ||
[cambium.core :as clog] | ||
[clojure.tools.logging :as log]) | ||
(:import (com.rabbitmq.client Connection) | ||
(org.apache.commons.pool2.impl GenericObjectPool GenericObjectPoolConfig) | ||
(java.time Duration) | ||
(gojek.rabbitmq.channel_pool RabbitMQChannelFactory))) | ||
|
||
(defn calc-total-thread-count [] | ||
(let [rmq-thread-count (c/total-thread-count) | ||
stream-router-config (get (zc/ziggurat-config) :stream-router) | ||
batch-routes-config (get (zc/ziggurat-config) :batch-routes) | ||
batch-consumer-thread-count (reduce (fn [sum config] | ||
(+ sum (:thread-count config))) 0 (vals batch-routes-config)) | ||
stream-thread-count (reduce (fn [sum config] | ||
(+ sum (:stream-threads-count config))) 0 (vals stream-router-config))] | ||
(clog/info {:channel-threads rmq-thread-count | ||
:batch-consumer-threads batch-consumer-thread-count | ||
:stream-threads stream-thread-count} "Thread counts") | ||
(+ stream-thread-count rmq-thread-count batch-consumer-thread-count))) | ||
|
||
(defn create-object-pool-config [config] | ||
(let [standby-size 10 | ||
total-thread-count (calc-total-thread-count) | ||
merged-config (merge {:max-wait-ms 5000 :min-idle standby-size :max-idle total-thread-count} config)] | ||
(doto (GenericObjectPoolConfig.) | ||
(.setMaxWait (Duration/ofMillis (:max-wait-ms merged-config))) | ||
(.setMinIdle (:min-idle merged-config)) | ||
(.setMaxIdle (:max-idle merged-config)) | ||
(.setMaxTotal (+ (:min-idle merged-config) total-thread-count)) | ||
(.setTestOnBorrow true) | ||
(.setJmxNamePrefix "zig-rabbitmq-ch-pool")))) | ||
|
||
(defn create-channel-pool [^Connection connection] | ||
(let [pool-config (create-object-pool-config (get-in zc/ziggurat-config [:rabbit-mq-connection :channel-pool])) | ||
rmq-chan-pool (GenericObjectPool. (RabbitMQChannelFactory. connection) pool-config)] | ||
rmq-chan-pool)) | ||
|
||
(defn destroy-channel-pool [channel-pool] | ||
(.close channel-pool)) | ||
|
||
(declare channel-pool) | ||
|
||
(defstate channel-pool | ||
:start (do (log/info "Creating channel pool") | ||
(create-channel-pool c/producer-connection)) | ||
:stop (do (log/info "Stopping channel pool") | ||
(destroy-channel-pool channel-pool))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.