com.tvd12.ezymq.mosquitto.manager.EzyMosquittoRpcProducerManager Maven / Gradle / Ivy
The newest version!
package com.tvd12.ezymq.mosquitto.manager;
import com.tvd12.ezyfox.codec.EzyEntityCodec;
import com.tvd12.ezyfox.util.EzyCloseable;
import com.tvd12.ezymq.mosquitto.EzyMosquittoRpcProducer;
import com.tvd12.ezymq.mosquitto.endpoint.EzyMosquittoRpcClient;
import com.tvd12.ezymq.mosquitto.endpoint.EzyMqttClientProxy;
import com.tvd12.ezymq.mosquitto.setting.EzyMosquittoRpcProducerSetting;
import java.util.HashMap;
import java.util.Map;
public class EzyMosquittoRpcProducerManager
extends EzyMosquittoAbstractManager
implements EzyCloseable {
protected final EzyEntityCodec entityCodec;
protected final Map rpProducers;
protected final Map rpcProducerSettings;
public EzyMosquittoRpcProducerManager(
EzyMqttClientProxy mqttClient,
EzyEntityCodec entityCodec,
Map rpcProducerSettings
) {
super(mqttClient);
this.entityCodec = entityCodec;
this.rpcProducerSettings = rpcProducerSettings;
this.rpProducers = createRpcProducers();
}
public EzyMosquittoRpcProducer getRpcProducer(String name) {
EzyMosquittoRpcProducer producer = rpProducers.get(name);
if (producer == null) {
throw new IllegalArgumentException(
"has no rpc producer with name: " + name
);
}
return producer;
}
protected Map createRpcProducers() {
Map map = new HashMap<>();
for (String name : rpcProducerSettings.keySet()) {
EzyMosquittoRpcProducerSetting setting = rpcProducerSettings.get(name);
map.put(
name,
createRpcProducer(name, setting)
);
}
return map;
}
protected EzyMosquittoRpcProducer createRpcProducer(
String name,
EzyMosquittoRpcProducerSetting setting
) {
try {
return createRpcProducer(setting);
} catch (Exception e) {
throw new IllegalStateException(
"create rpc producer: " + name + " error",
e
);
}
}
protected EzyMosquittoRpcProducer createRpcProducer(
EzyMosquittoRpcProducerSetting setting
) throws Exception {
String topic = setting.getTopic();
String replyTopic = setting.getReplyTopic();
EzyMosquittoRpcClient client = EzyMosquittoRpcClient
.builder()
.topic(topic)
.replyTopic(replyTopic)
.mqttClient(mqttClient)
.capacity(setting.getCapacity())
.defaultTimeout(setting.getDefaultTimeout())
.correlationIdFactory(setting.getCorrelationIdFactory())
.unconsumedResponseConsumer(setting.getUnconsumedResponseConsumer())
.build();
EzyMosquittoRpcProducer producer = EzyMosquittoRpcProducer
.builder()
.entityCodec(entityCodec)
.client(client)
.build();
mqttClient.subscribe(replyTopic);
return producer;
}
public void close() {
for (EzyMosquittoRpcProducer producer : rpProducers.values()) {
producer.close();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy