-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
4ea2977
commit 187cbd0
Showing
10 changed files
with
523 additions
and
77 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
66 changes: 66 additions & 0 deletions
66
distmq-client/src/main/java/com/github/wenweihu86/distmq/client/BrokerClient.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,66 @@ | ||
package com.github.wenweihu86.distmq.client; | ||
|
||
import com.github.wenweihu86.distmq.client.api.BrokerAPI; | ||
import com.github.wenweihu86.rpc.client.RPCClient; | ||
import com.github.wenweihu86.rpc.client.RPCClientOptions; | ||
import com.github.wenweihu86.rpc.client.RPCProxy; | ||
import org.apache.commons.lang.builder.EqualsBuilder; | ||
import org.apache.commons.lang.builder.HashCodeBuilder; | ||
|
||
/** | ||
* Created by wenweihu86 on 2017/6/24. | ||
*/ | ||
public class BrokerClient { | ||
private String address; | ||
private RPCClient rpcClient; | ||
private BrokerAPI brokerAPI; | ||
|
||
public BrokerClient(String address, RPCClientOptions options) { | ||
this.address = address; | ||
this.rpcClient = new RPCClient(address, options); | ||
this.brokerAPI = RPCProxy.getProxy(this.rpcClient, BrokerAPI.class); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
boolean flag = false; | ||
if (object != null && BrokerClient.class.isAssignableFrom(object.getClass())) { | ||
BrokerClient rhs = (BrokerClient) object; | ||
flag = new EqualsBuilder() | ||
.append(address, rhs.address) | ||
.isEquals(); | ||
} | ||
return flag; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return new HashCodeBuilder() | ||
.append(address) | ||
.toHashCode(); | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(String address) { | ||
this.address = address; | ||
} | ||
|
||
public RPCClient getRpcClient() { | ||
return rpcClient; | ||
} | ||
|
||
public void setRpcClient(RPCClient rpcClient) { | ||
this.rpcClient = rpcClient; | ||
} | ||
|
||
public BrokerAPI getBrokerAPI() { | ||
return brokerAPI; | ||
} | ||
|
||
public void setBrokerAPI(BrokerAPI brokerAPI) { | ||
this.brokerAPI = brokerAPI; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
distmq-client/src/main/java/com/github/wenweihu86/distmq/client/BrokerClientManager.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,39 @@ | ||
package com.github.wenweihu86.distmq.client; | ||
|
||
import com.github.wenweihu86.rpc.client.RPCClientOptions; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* Created by wenweihu86 on 2017/6/24. | ||
*/ | ||
public class BrokerClientManager { | ||
private static BrokerClientManager instance; | ||
private static RPCClientOptions rpcClientOptions; | ||
private ConcurrentMap<String, BrokerClient> brokerClientMap = new ConcurrentHashMap<>(); | ||
|
||
public static BrokerClientManager getInstance() { | ||
if (instance == null) { | ||
instance = new BrokerClientManager(); | ||
} | ||
return instance; | ||
} | ||
|
||
public static RPCClientOptions getRpcClientOptions() { | ||
return rpcClientOptions; | ||
} | ||
|
||
public static void setRpcClientOptions(RPCClientOptions rpcClientOptions) { | ||
BrokerClientManager.rpcClientOptions = rpcClientOptions; | ||
} | ||
|
||
public ConcurrentMap<String, BrokerClient> getBrokerClientMap() { | ||
return brokerClientMap; | ||
} | ||
|
||
public void setBrokerClientMap(ConcurrentMap<String, BrokerClient> brokerClientMap) { | ||
this.brokerClientMap = brokerClientMap; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
distmq-client/src/main/java/com/github/wenweihu86/distmq/client/CommonConfig.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,45 @@ | ||
package com.github.wenweihu86.distmq.client; | ||
|
||
import com.github.wenweihu86.distmq.client.zk.ZKConf; | ||
import com.github.wenweihu86.rpc.client.RPCClientOptions; | ||
|
||
/** | ||
* Created by wenweihu86 on 2017/6/24. | ||
*/ | ||
public class CommonConfig extends ZKConf { | ||
private int brokerConnectTimeoutMs = 200; | ||
private int brokerReadTimeoutMs = 500; | ||
private int brokerWriteTimeoutMs = 200; | ||
|
||
public RPCClientOptions getRPCClientOptions() { | ||
RPCClientOptions rpcClientOptions = new RPCClientOptions(); | ||
rpcClientOptions.setConnectTimeoutMillis(brokerConnectTimeoutMs); | ||
rpcClientOptions.setReadTimeoutMillis(brokerReadTimeoutMs); | ||
rpcClientOptions.setWriteTimeoutMillis(brokerWriteTimeoutMs); | ||
return rpcClientOptions; | ||
} | ||
|
||
public int getBrokerConnectTimeoutMs() { | ||
return brokerConnectTimeoutMs; | ||
} | ||
|
||
public void setBrokerConnectTimeoutMs(int brokerConnectTimeoutMs) { | ||
this.brokerConnectTimeoutMs = brokerConnectTimeoutMs; | ||
} | ||
|
||
public int getBrokerReadTimeoutMs() { | ||
return brokerReadTimeoutMs; | ||
} | ||
|
||
public void setBrokerReadTimeoutMs(int brokerReadTimeoutMs) { | ||
this.brokerReadTimeoutMs = brokerReadTimeoutMs; | ||
} | ||
|
||
public int getBrokerWriteTimeoutMs() { | ||
return brokerWriteTimeoutMs; | ||
} | ||
|
||
public void setBrokerWriteTimeoutMs(int brokerWriteTimeoutMs) { | ||
this.brokerWriteTimeoutMs = brokerWriteTimeoutMs; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
distmq-client/src/main/java/com/github/wenweihu86/distmq/client/consumer/ConsumerConfig.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,9 @@ | ||
package com.github.wenweihu86.distmq.client.consumer; | ||
|
||
import com.github.wenweihu86.distmq.client.CommonConfig; | ||
|
||
/** | ||
* Created by wenweihu86 on 2017/6/24. | ||
*/ | ||
public class ConsumerConfig extends CommonConfig { | ||
} |
100 changes: 100 additions & 0 deletions
100
distmq-client/src/main/java/com/github/wenweihu86/distmq/client/producer/Producer.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,100 @@ | ||
package com.github.wenweihu86.distmq.client.producer; | ||
|
||
import com.github.wenweihu86.distmq.client.BrokerClient; | ||
import com.github.wenweihu86.distmq.client.BrokerClientManager; | ||
import com.github.wenweihu86.distmq.client.api.BrokerMessage; | ||
import com.github.wenweihu86.distmq.client.zk.ZKClient; | ||
import com.github.wenweihu86.distmq.client.zk.ZKData; | ||
import com.google.protobuf.ByteString; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
/** | ||
* Created by wenweihu86 on 2017/6/24. | ||
*/ | ||
public class Producer { | ||
private static final Logger LOG = LoggerFactory.getLogger(Producer.class); | ||
private ProducerConfig config; | ||
private ZKClient zkClient; | ||
|
||
public Producer(ProducerConfig config) { | ||
this.config = config; | ||
BrokerClientManager.setRpcClientOptions(this.config.getRPCClientOptions()); | ||
zkClient = new ZKClient(config); | ||
zkClient.subscribeBroker(); | ||
zkClient.subscribeTopic(); | ||
} | ||
|
||
public boolean send(String topic, byte[] messageBytes) { | ||
ZKData zkData = ZKData.getInstance(); | ||
Map<Integer, Integer> queueMap; | ||
zkData.getTopicLock().lock(); | ||
try { | ||
queueMap = zkData.getTopicMap().get(topic); | ||
} finally { | ||
zkData.getTopicLock().unlock(); | ||
} | ||
Integer queueId; | ||
Integer shardingId; | ||
if (queueMap == null) { | ||
zkClient.registerTopic(topic, config.getQueueCountPerTopic()); | ||
zkData.getTopicLock().lock(); | ||
try { | ||
while (!zkData.getTopicMap().containsKey(topic)) { | ||
zkData.getTopicCondition().awaitUninterruptibly(); | ||
} | ||
} finally { | ||
zkData.getTopicLock().unlock(); | ||
} | ||
} | ||
|
||
zkData.getTopicLock().lock(); | ||
try { | ||
queueMap = zkData.getTopicMap().get(topic); | ||
int queueCount = queueMap.size(); | ||
int randomIndex = ThreadLocalRandom.current().nextInt(0, queueCount); | ||
Integer[] queueArray = queueMap.keySet().toArray(new Integer[0]); | ||
queueId = queueArray[randomIndex]; | ||
shardingId = queueMap.get(queueId); | ||
} finally { | ||
zkData.getTopicLock().unlock(); | ||
} | ||
|
||
// send message to broker | ||
BrokerMessage.SendMessageRequest request = BrokerMessage.SendMessageRequest.newBuilder() | ||
.setTopic(topic) | ||
.setQueue(queueId) | ||
.setContent(ByteString.copyFrom(messageBytes)) | ||
.build(); | ||
|
||
List<String> brokerAddressList; | ||
zkData.getBrokerLock().lock(); | ||
try { | ||
brokerAddressList = zkData.getBrokerMap().get(shardingId); | ||
} finally { | ||
zkData.getBrokerLock().unlock(); | ||
} | ||
int randIndex = ThreadLocalRandom.current().nextInt(0, brokerAddressList.size()); | ||
String brokerAddress = brokerAddressList.get(randIndex); | ||
BrokerClient brokerClient = BrokerClientManager.getInstance().getBrokerClientMap().get(brokerAddress); | ||
BrokerMessage.SendMessageResponse response = brokerClient.getBrokerAPI().sendMessage(request); | ||
if (response == null || response.getBaseRes().getResCode() != BrokerMessage.ResCode.RES_CODE_SUCCESS) { | ||
LOG.warn("send message failed, topic={}, queue={}, brokerAddress={}", | ||
topic, queueId, brokerAddress); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public ProducerConfig getConfig() { | ||
return config; | ||
} | ||
|
||
public void setConfig(ProducerConfig config) { | ||
this.config = config; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
distmq-client/src/main/java/com/github/wenweihu86/distmq/client/producer/ProducerConfig.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,18 @@ | ||
package com.github.wenweihu86.distmq.client.producer; | ||
|
||
import com.github.wenweihu86.distmq.client.CommonConfig; | ||
|
||
/** | ||
* Created by wenweihu86 on 2017/6/24. | ||
*/ | ||
public class ProducerConfig extends CommonConfig { | ||
private int queueCountPerTopic = 4; | ||
|
||
public int getQueueCountPerTopic() { | ||
return queueCountPerTopic; | ||
} | ||
|
||
public void setQueueCountPerTopic(int queueCountPerTopic) { | ||
this.queueCountPerTopic = queueCountPerTopic; | ||
} | ||
} |
Oops, something went wrong.