lambda高級應用 可以裝逼了


閱讀HystrixGatewayFilterFactory的apply方法百思不得姐 后來自己寫了一遍 終於弄明白了 這個是lambda的一種高級應用 特留書於此 做個記錄

首先該方法的返回值是GatewayFilter 一個接口 返回值卻如下
return (exchange, chain) -> {
			RouteHystrixCommand command = new RouteHystrixCommand(config.setter, config.fallbackUri, exchange, chain);

			return Mono.create(s -> {
				Subscription sub = command.toObservable().subscribe(s::success, s::error, s::success);
				s.onCancel(sub::unsubscribe);
			}).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> {
				if (throwable instanceof HystrixRuntimeException) {
					HystrixRuntimeException e = (HystrixRuntimeException) throwable;
					if (e.getFailureType() == TIMEOUT) { //TODO: optionally set status
						setResponseStatus(exchange, HttpStatus.GATEWAY_TIMEOUT);
						return exchange.getResponse().setComplete();
					}
				}
				return Mono.empty();
			}).then();
		};

  

怎么看也不是GatewayFilter對象 之后弄明白了 
是使用了一個內部類 直接實現了GatewayFilter接口 其中 有個方法如下
Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
返回值是Mono 所以在{}中實現了此方法 但是只有接口中單個方法的時候可以用

以后試一下接口中多個方法的時候 如何使用lambda表達式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM