This repository has been archived by the owner on May 2, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor connectors 3rd party to use constructor injection instead of field inject and use strategy pattern to select the call strategy (with or without SLA). Had to downgrade failsafe version due to spring-projects/spring-boot#6254
- Loading branch information
1 parent
757dbe8
commit 707ccc1
Showing
17 changed files
with
433 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...d-party/src/main/java/org/activiti/cloud/connectors/external/BasicShoutServiceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2017 Alfresco, Inc. and/or its affiliates. | ||
* | ||
* 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 org.activiti.cloud.connectors.external; | ||
|
||
import org.activiti.cloud.connectors.external.model.Shout; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
public class BasicShoutServiceClient implements ShoutServiceClient { | ||
|
||
private static final String SHOUT_SERVICE_URL = "http://API.SHOUTCLOUD.IO/V1/SHOUT"; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
public BasicShoutServiceClient(RestTemplate restTemplate) { | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
public Shout shout(String tweet) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
HttpEntity<Shout> request = new HttpEntity<>(new Shout(tweet), | ||
headers); | ||
ResponseEntity<Shout> response = restTemplate | ||
.exchange(SHOUT_SERVICE_URL, | ||
HttpMethod.POST, | ||
request, | ||
Shout.class); | ||
|
||
if (!response.getStatusCode().is2xxSuccessful()) { | ||
throw new IllegalStateException("Error Calling my shouty service for tweet: " + tweet); | ||
} | ||
|
||
return response.getBody(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...arty/src/main/java/org/activiti/cloud/connectors/external/RequestRateLimiterProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2017 Alfresco, Inc. and/or its affiliates. | ||
* | ||
* 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 org.activiti.cloud.connectors.external; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; | ||
import es.moki.ratelimitj.core.limiter.request.RequestRateLimiter; | ||
import es.moki.ratelimitj.inmemory.request.InMemorySlidingWindowRequestRateLimiter; | ||
import org.activiti.cloud.connectors.external.config.SLAProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class RequestRateLimiterProvider { | ||
|
||
private final SLAProperties slaProperties; | ||
|
||
private RequestRateLimiter requestRateLimiter; | ||
|
||
public RequestRateLimiterProvider(SLAProperties slaProperties) { | ||
this.slaProperties = slaProperties; | ||
} | ||
|
||
public RequestRateLimiter getRequestRateLimiter(){ | ||
if (requestRateLimiter == null) { | ||
Set<RequestLimitRule> rules = Collections.singleton(RequestLimitRule.of(1, | ||
TimeUnit.MINUTES, | ||
slaProperties.getRequests())); // request per minute, per key | ||
requestRateLimiter = new InMemorySlidingWindowRequestRateLimiter(rules); | ||
System.out.println("Rate limit - postconstruct - rpm " + slaProperties.getRequests()); | ||
} | ||
return requestRateLimiter; | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...3rd-party/src/main/java/org/activiti/cloud/connectors/external/SLAShoutServiceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2017 Alfresco, Inc. and/or its affiliates. | ||
* | ||
* 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 org.activiti.cloud.connectors.external; | ||
|
||
import org.activiti.cloud.connectors.external.model.Shout; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class SLAShoutServiceClient implements ShoutServiceClient { | ||
|
||
private static final String SHOUT_SERVICE = "shoutApi"; | ||
|
||
private static final int WAIT_FOR_SLA = 1000; | ||
|
||
private final BasicShoutServiceClient basicShoutServiceClient; | ||
|
||
private final RequestRateLimiterProvider requestRateLimiterProvider; | ||
|
||
public SLAShoutServiceClient(BasicShoutServiceClient basicShoutServiceClient, | ||
RequestRateLimiterProvider requestRateLimiterProvider) { | ||
this.basicShoutServiceClient = basicShoutServiceClient; | ||
this.requestRateLimiterProvider = requestRateLimiterProvider; | ||
} | ||
|
||
@Override | ||
public Shout shout(String tweet) { | ||
boolean serviceRequestSent = false; | ||
Shout shout = null; | ||
|
||
while (!serviceRequestSent) { | ||
boolean slaOverLimit = requestRateLimiterProvider.getRequestRateLimiter().overLimitWhenIncremented(SHOUT_SERVICE); | ||
if (!slaOverLimit) { | ||
|
||
shout = basicShoutServiceClient.shout(tweet); | ||
serviceRequestSent = true; | ||
} else { | ||
System.out.println(">> Waiting for SLAs allowance ..."); | ||
try { | ||
Thread.sleep(WAIT_FOR_SLA); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
return shout; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...rs-3rd-party/src/main/java/org/activiti/cloud/connectors/external/ShoutServiceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2017 Alfresco, Inc. and/or its affiliates. | ||
* | ||
* 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 org.activiti.cloud.connectors.external; | ||
|
||
import org.activiti.cloud.connectors.external.model.Shout; | ||
|
||
public interface ShoutServiceClient { | ||
|
||
Shout shout(String tweet); | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...arty/src/main/java/org/activiti/cloud/connectors/external/ShoutServiceClientSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2017 Alfresco, Inc. and/or its affiliates. | ||
* | ||
* 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 org.activiti.cloud.connectors.external; | ||
|
||
import org.activiti.cloud.connectors.external.config.SLAProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ShoutServiceClientSelector { | ||
|
||
private final SLAProperties slaProperties; | ||
|
||
private final BasicShoutServiceClient basicShoutServiceClient; | ||
|
||
private final SLAShoutServiceClient slaShoutServiceClient; | ||
|
||
public ShoutServiceClientSelector(SLAProperties slaProperties, | ||
BasicShoutServiceClient basicShoutServiceClient, | ||
SLAShoutServiceClient slaShoutServiceClient) { | ||
this.slaProperties = slaProperties; | ||
this.basicShoutServiceClient = basicShoutServiceClient; | ||
this.slaShoutServiceClient = slaShoutServiceClient; | ||
} | ||
|
||
public ShoutServiceClient select() { | ||
return slaProperties.isEnabled()? slaShoutServiceClient : basicShoutServiceClient; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.