All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gu.dtalk.cmd.TaskManager Maven / Gradle / Ivy

There is a newer version: 0.6.9
Show newest version
package gu.dtalk.cmd;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import gu.dtalk.Ack;
import gu.dtalk.DeviceInstruction;
import gu.dtalk.IAckAdapter;
import gu.dtalk.exception.AckTimtoutException;
import gu.simplemq.Channel;
import gu.simplemq.IProducer;
import gu.simplemq.IPublisher;
import gu.simplemq.ISubscriber;
import static com.google.common.base.Preconditions.*;

import java.util.List;
import java.util.Map;

import com.google.common.base.Strings;

/**
 * 
 * (任务队列)设备命令发送管理模块
* @author guyadong * */ public class TaskManager extends BaseCmdManager { private final IProducer producer; private Supplier> taskQueueSupplier; private String cmdpath; /** * 构造方法 * @param publisher 消息发布器 * @param subscriber 消息订阅(接收)发布器 * @param producer 生产者 */ public TaskManager(IPublisher publisher,ISubscriber subscriber,IProducer producer) { super(publisher,subscriber); this.producer = checkNotNull(producer,"producer is null"); } /** * 构造方法 * @param publisher 消息发布器 * @param subscriber 消息订阅(接收)发布器 * @param producer 生产者 * @param taskQueueSupplier */ public TaskManager(IPublisher publisher,ISubscriber subscriber,IProducer producer, Supplier taskQueueSupplier) { this(publisher,subscriber,producer); this.taskQueueSupplier = new FreshedChannelSupplier(taskQueueSupplier); } public TaskManager(IPublisher publisher,ISubscriber subscriber,IProducer producer, String cmdpath, Supplier taskQueueSupplier) { this(publisher,subscriber,producer,taskQueueSupplier); setCmdpath(cmdpath); } /** * @return cmdpath */ public String getCmdpath() { return cmdpath; } /** * @param cmdpath 要设置的 cmdpath * @return 当前对象 */ public TaskManager setCmdpath(String cmdpath) { this.cmdpath = checkNotNull(Strings.emptyToNull(cmdpath),"cmdpath is null or empty"); return this; } /** * @return channelSupplier */ public Supplier> getTaskQueueSupplier() { return taskQueueSupplier; } /** * @param taskQueueSupplier 要设置的 channelSupplier * @return 当前对象 */ public TaskManager setTaskQueueSupplier(Supplier> taskQueueSupplier) { this.taskQueueSupplier = checkNotNull(taskQueueSupplier,"taskQueueSupplier is null"); return this; } /** * @param taskQueue 任务队列名 * @return 当前对象 */ public TaskManager setTaskQueue(String taskQueue) { Channel channel = new Channel<>(checkNotNull(Strings.emptyToNull(taskQueue),"taskQueue is null or empty"),DeviceInstruction.class); return setTaskQueueSupplier(Suppliers.ofInstance(channel)); } /** * 发送设备命令 * @param cmd 设备命令 * @return 1--成功提交任务,0--任务提交失败 */ @Override protected long doSendCmd(DeviceInstruction cmd){ Channel channel = checkNotNull(taskQueueSupplier,"taskQueueSupplier is uninitialized").get(); int numSub = producer.getAdvisor().consumerCountOf(checkNotNull(channel,"taskQueue from taskQueueSupplier is null ").name); if(numSub >0){ producer.produce(channel, cmd); return 1; } return 0; } private String checkCmdpath(){ return checkNotNull(cmdpath,"'cmdpath' field is uninitialized"); } private void checkCmdPath(String cmdpath){ checkState(this.checkCmdpath().equals(cmdpath),"MISMATCH argument cmdpath,required %s",this.checkCmdpath()); } /** * @see gu.dtalk.cmd.BaseCmdManager#runCmd(java.lang.String, java.util.Map) * @deprecated replaced by {@link #runCmd(Map)} */ @Override public int runCmd(String cmdpath, Map params) { checkCmdPath(cmdpath); return super.runCmd(cmdpath, params); } /** * @see gu.dtalk.cmd.BaseCmdManager#runCmd(java.lang.String, java.util.Map, gu.dtalk.IAckAdapter) * @deprecated replaced by {@link #runCmd(Map, IAckAdapter)} */ @Override public void runCmd(String cmdpath, Map params, IAckAdapter adapter) { checkCmdPath(cmdpath); super.runCmd(cmdpath, params, adapter); } /** * @see gu.dtalk.cmd.BaseCmdManager#runCmdSync(java.lang.String, java.util.Map, boolean) * @deprecated replaced by {@link #runCmdSync(Map, boolean)} */ @Override public List> runCmdSync(String cmdpath, Map params, boolean throwIfTimeout) throws InterruptedException, AckTimtoutException { checkCmdPath(cmdpath); return super.runCmdSync(cmdpath, params, throwIfTimeout); } /** * 发送设备命令
* @param params 命令参数 * @return 任务成功提交返回{@code true},否则返回{@code false} * @see gu.dtalk.cmd.BaseCmdManager#runCmd(java.lang.String, java.util.Map) */ public boolean runCmd(Map params) { return 1 == super.runCmd(checkCmdpath(), params); } /** * @param params - * @param adapter - * @see gu.dtalk.cmd.BaseCmdManager#runCmd(java.lang.String, java.util.Map, gu.dtalk.IAckAdapter) */ public void runCmd(Map params, IAckAdapter adapter) { super.runCmd(checkCmdpath(), params, adapter); } /** * @param params - * @param throwIfTimeout - * @return - * @throws InterruptedException - * @throws AckTimtoutException - * @see gu.dtalk.cmd.BaseCmdManager#runCmdSync(java.lang.String, java.util.Map, boolean) */ public List> runCmdSync(Map params, boolean throwIfTimeout) throws InterruptedException, AckTimtoutException { return super.runCmdSync(checkCmdpath(), params, throwIfTimeout); } }