This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
DefaultServiceLocationService.java
99 lines (88 loc) · 3.36 KB
/
DefaultServiceLocationService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package demo.service.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.hateoas.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import demo.model.CurrentPosition;
import demo.model.ServiceLocation;
import demo.model.VehicleStatus;
import demo.service.ServiceLocationService;
/**
*
* @author Gunnar Hillert
*
*/
@Service
public class DefaultServiceLocationService implements ServiceLocationService {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultServiceLocationService.class);
@Autowired
@LoadBalanced
private RestTemplate restTemplate;
/**
* Enriches the provided {@link CurrentPosition} with the closest service location
* IF the {@link CurrentPosition} has a {@link VehicleStatus} of:
*
* <ul>
* <li>{@link VehicleStatus#SERVICE_NOW}</li>
* <li>{@link VehicleStatus#SERVICE_SOON}</li>
* <li>{@link VehicleStatus#STOP_NOW}</li>
* <ul>
*
* @param currentPosition Will be enriched with the closest service location
* @throws Exception
*/
@HystrixCommand(
commandKey="serviceLocations",
commandProperties = {
//@HystrixProperty(name = "circuitBreaker.forceOpen", value = "true")
},
fallbackMethod = "handleServiceLocationServiceFailure")
@Override
public void updateServiceLocations(CurrentPosition currentPosition) {
switch (currentPosition.getVehicleStatus()) {
case SERVICE_NOW:
case SERVICE_SOON:
case STOP_NOW:
ResponseEntity<Resource<ServiceLocation>> result = this.restTemplate.exchange(
"http://service-location-service/serviceLocations/search/findFirstByLocationNear?location={lat},{long}",
HttpMethod.GET, new HttpEntity<Void>((Void) null),
new ParameterizedTypeReference<Resource<ServiceLocation>>() {
}, currentPosition.getLocation().getLatitude(),
currentPosition.getLocation().getLongitude());
if (result.getStatusCode() == HttpStatus.OK
&& result.getBody().getContent() != null) {
currentPosition.setServiceLocation(result.getBody().getContent());
}
break;
default:
}
}
public void handleServiceLocationServiceFailure(CurrentPosition currentPosition) {
LOGGER.error("Hystrix Fallback Method. Unable to retrieve service station info.");
}
}