Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
Refactor connectors 3rd party (#43)
Browse files Browse the repository at this point in the history
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
erdemedeiros authored and ryandawsonuk committed Dec 14, 2017
1 parent 757dbe8 commit 707ccc1
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 152 deletions.
Expand Up @@ -66,14 +66,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
Expand Down
@@ -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();
}

}
@@ -1,7 +1,23 @@
/*
* 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.activiti.cloud.connectors.starter.configuration.EnableActivitiCloudConnector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -12,11 +28,11 @@
@ComponentScan({"org.activiti.cloud.connectors.starter", "org.activiti.cloud.connectors.external"})
public class ExternalServiceCloudConnector implements CommandLineRunner {

@Value("${sla.requests}")
private int requestPerMinute;
private final SLAProperties slaProperties;

@Value("${sla.enabled}")
private boolean slaEnabled;
public ExternalServiceCloudConnector(SLAProperties slaProperties) {
this.slaProperties = slaProperties;
}

public static void main(String[] args) {
SpringApplication.run(ExternalServiceCloudConnector.class,
Expand All @@ -25,6 +41,6 @@ public static void main(String[] args) {

@Override
public void run(String... args) throws Exception {
System.out.println("Starting SLA Requests with: RPM -> " + requestPerMinute + " and Enabled: " + slaEnabled);
System.out.println("Starting SLA Requests with: RPM -> " + slaProperties.getRequests() + " and Enabled: " + slaProperties.isEnabled());
}
}
@@ -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;
}

}
@@ -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;
}

}
@@ -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);

}
@@ -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;
}

}
Expand Up @@ -6,7 +6,6 @@
import org.activiti.cloud.connectors.starter.channels.CloudConnectorChannels;
import org.activiti.cloud.connectors.starter.model.IntegrationRequestEvent;
import org.activiti.cloud.connectors.starter.model.IntegrationResultEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
Expand All @@ -15,10 +14,10 @@
@Component
public class TweetAnalyzerConnector {

@Autowired
private MessageChannel integrationResultsProducer;
private final MessageChannel integrationResultsProducer;

public TweetAnalyzerConnector() {
public TweetAnalyzerConnector(MessageChannel integrationResultsProducer) {
this.integrationResultsProducer = integrationResultsProducer;
}

@StreamListener(value = CloudConnectorChannels.INTEGRATION_EVENT_CONSUMER, condition = "headers['connectorType']=='Analyze English Tweet'")
Expand Down

0 comments on commit 707ccc1

Please sign in to comment.