责任链设计模式学习
1,xml设置
<?xml version="1.0" encoding="UTF-8"?> <catalog> <chain name="myChaia" className="com.zr.alibaba.template.chain.test"> <command id="B" className="com.zr.alibaba.template.chain.test.chain.command.SetTestCommand" /> <command id="C" className="com.zr.alibaba.template.chain.teste.chain.command.SetTest2Command" /> </chain> </catalog>
2,基础命令
public interface BaseCommand extends Command<String, Object, JSONObject> { /** * 覆盖执行方法 * * @param context * @return */ @Override default Processing execute1(JSONObject context) { this.doExecute(context); return Processing.CONTINUE; } /** * 实际执行方法 * * @param context */ void doExecute2(JSONObject context); }
3,基础链
public class BaseChain extends ChainBase<String, Object, JSONObject> { @Override public Processing execute(JSONObject context) { try { return super.execute(context); } catch (ChainException e) { log.error(e.getMessage(), e); throw new RuntimeException(Optional.ofNullable(e.getCause()).map(ExceptionUtil::getMessage) .orElse(e.getMessage())); } } }
4,加载配置这个要用到springbot的自动加载 spring.factories 来加载文件全限定包名
TaskConfig
package com.bessky.common.task; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import org.apache.commons.chain2.config.xml.XmlConfigParser; import org.apache.commons.digester3.Digester; import org.apache.commons.digester3.ObjectCreateRule; import org.apache.commons.digester3.RuleSet; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.xml.sax.Attributes; import javax.annotation.Resource; import java.net.URL; import java.util.Enumeration; @Configuration public class TaskConfig implements InitializingBean { @Resource private ApplicationContext applicationContext; @Bean @ConfigurationProperties("xxl.job.executor") @ConditionalOnProperty("xxl.job.executor.adminaddresses") public XxlJobSpringExecutor xxlJobExecutor() { return new XxlJobSpringExecutor(); } @Override public void afterPropertiesSet() throws Exception { Enumeration<URL> enumeration = this.getClass().getClassLoader().getResources("chain-config.xml"); if (!enumeration.hasMoreElements()) { return; } XmlConfigParser parser = new XmlConfigParser() { @Override public Digester getDigester() { Digester digester = new Digester() { @Override public void addObjectCreate(String pattern, String className, String attributeName) { addRule(pattern, new ObjectCreateRule(className, attributeName) { @Override public void begin(String namespace, String name, Attributes attributes) throws Exception { String className = attributes.getValue(this.attributeName); if (className != null) { Class<?> clazz = this.getDigester().getClassLoader().loadClass(className); Object object = applicationContext.getBean(clazz); this.getDigester().push(object); return; } super.begin(namespace, name, attributes); } }); } }; RuleSet ruleSet = this.getRuleSet(); digester.setNamespaceAware(ruleSet.getNamespaceURI() != null); digester.setUseContextClassLoader(this.getUseContextClassLoader()); digester.setValidating(false); digester.addRuleSet(ruleSet); return digester; } }; do { parser.parse(enumeration.nextElement()); } while (enumeration.hasMoreElements()); } }
一般定时任务里面就可以调用自定义业务链chain
MyJobHandler etends BaseJobHandler----> MyChain .execute(用上下文set值)---->会自动执行多个conmmond