函数式编程的好处:
减少可变量的声明,能够更好的利用并行,代码更加简洁可读。
下面来讲讲我怎么通过函数式来实现一个工厂:
首先,创建一个函数接口:
@FunctionalInterface public interface INotifyFactoryService { void notifyFactory(TagInfoResponse tagInfoResponse,String result) throws IOException; }
public class NotifyFactoryService { private static final Logger LOG = LoggerFactory.getLogger(NotifyFactoryService.class); private static Map<Integer,INotifyFactoryService> notifyFactoryServiceMap = new ConcurrentHashMap<>(); private CamelContext camelContext; public void init(){ LOG.info("【init】:"+0); notifyFactoryServiceMap.put(0,(tagInfoResponse,result)->{ /** * 写MQ */ Exchange exchg = new DefaultExchange(camelContext); exchg.setProperty(Constant.STYTEM_APPKEY,tagInfoResponse.getSubject()); exchg.setProperty(Constant.STYTEM_MQNAME,tagInfoResponse.getSubject() + "_"+tagInfoResponse.getTag()); exchg.setProperty(Constant.SYSTEM_TAGS,tagInfoResponse.getTag()); exchg.getIn().setBody(result); LOG.info("【发送到路由】: direct:_PaymentNotifyToMQ"); camelContext.createProducerTemplate().send("direct:_PaymentNotifyToMQ",exchg); }); LOG.info("【init】:"+1); notifyFactoryServiceMap.put(1,(tagInfoResponse, result) -> { /** * 主动请求回调 */ Exchange exchg = new DefaultExchange(camelContext); exchg.setPattern(ExchangePattern.InOut); exchg.getIn().setHeader(Exchange.HTTP_URI,tagInfoResponse.getCallbackUrl()); exchg.getIn().setBody(result); LOG.info("【发送到路由】: direct:_bizCallBack"); camelContext.createProducerTemplate().send("direct:_bizCallBack",exchg); final Integer code = exchg.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class); LOG.info("code:{}",code); if (code != 200 && code != 201) { LOG.error("调用报错,服务端返回code = " + code); return; } final String responseText = IOUtils.toString(exchg.getOut().getBody(InputStream.class), "UTF-8"); LOG.info("responseText:{}",responseText); }); LOG.info("【init】:"+11); notifyFactoryServiceMap.put(11,((tagInfoResponse, result) -> { /** * 写MQ * 主动请求回调 */ Exchange exchg = new DefaultExchange(camelContext); exchg.setProperty(Constant.STYTEM_APPKEY,tagInfoResponse.getSubject()); exchg.setProperty(Constant.STYTEM_MQNAME,tagInfoResponse.getSubject() + "_"+tagInfoResponse.getTag()); exchg.setProperty(Constant.SYSTEM_TAGS,tagInfoResponse.getTag()); exchg.getIn().setBody(result); LOG.info("【发送到路由】: direct:_PaymentNotifyToMQ"); camelContext.createProducerTemplate().send("direct:_PaymentNotifyToMQ",exchg); Exchange exchg1 = new DefaultExchange(camelContext); exchg1.setPattern(ExchangePattern.InOut); exchg1.getIn().setHeader(Exchange.HTTP_URI,tagInfoResponse.getCallbackUrl()); exchg1.getIn().setBody(result); LOG.info("【发送到路由】: direct:_bizCallBack"); camelContext.createProducerTemplate().send("direct:_bizCallBack",exchg1); })); LOG.info("【init】:"+9); notifyFactoryServiceMap.put(9,((tagInfoResponse, result) -> { /** * 什么都不写 */ })); } public static INotifyFactoryService getNotifyStrategy(Integer type){ return notifyFactoryServiceMap.get(type); } public void setCamelContext(CamelContext camelContext) { this.camelContext = camelContext; } }
注意:以上init()方法在bundle启动的是就去加载,就会创建好我们希望创建的服务。
//使用服务 INotifyFactoryService notifyFactoryService = NotifyFactoryService.getNotifyStrategy(1); notifyFactoryService.notifyFactory(tagInfoResponse,result);