From 2dbcf86f3d6ce77b2f9ecebff7982648d7fa1713 Mon Sep 17 00:00:00 2001 From: mharindu Date: Fri, 20 May 2016 15:52:50 +0530 Subject: [PATCH 1/2] Added retrying mechanism to webapp publisher --- .../publisher/APIPublisherStartupHandler.java | 49 +++++++++++++------ .../lifecycle/util/AnnotationProcessor.java | 3 +- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java index 30f7900153..02f75607fd 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java @@ -25,9 +25,17 @@ import org.wso2.carbon.apimgt.api.model.API; import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder; import org.wso2.carbon.core.ServerStartupObserver; +import java.util.Stack; + public class APIPublisherStartupHandler implements ServerStartupObserver { private static final Log log = LogFactory.getLog(APIPublisherStartupHandler.class); + private static int retryTime = 2000; + private static final int CONNECTION_RETRY_FACTOR = 2; + private static Stack failedAPIsStack = new Stack<>(); + private static Stack currentAPIsStack; + + private APIPublisherService publisher; @Override public void completingServerStartup() { @@ -36,32 +44,45 @@ public class APIPublisherStartupHandler implements ServerStartupObserver { @Override public void completedServerStartup() { - // adding temporary due to a bug in the platform + APIPublisherDataHolder.getInstance().setServerStarted(true); + currentAPIsStack = APIPublisherDataHolder.getInstance().getUnpublishedApis(); Thread t = new Thread(new Runnable() { @Override public void run() { - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - log.error("Error occurred while sleeping", e); - } - APIPublisherDataHolder.getInstance().setServerStarted(true); - log.info("Server has just started, hence started publishing unpublished APIs"); if (log.isDebugEnabled()) { + log.debug("Server has just started, hence started publishing unpublished APIs"); log.debug("Total number of unpublished APIs: " + APIPublisherDataHolder.getInstance().getUnpublishedApis().size()); } - APIPublisherService publisher = APIPublisherDataHolder.getInstance().getApiPublisherService(); - while (!APIPublisherDataHolder.getInstance().getUnpublishedApis().isEmpty()) { - API api = APIPublisherDataHolder.getInstance().getUnpublishedApis().pop(); + publisher = APIPublisherDataHolder.getInstance().getApiPublisherService(); + while (!failedAPIsStack.isEmpty() || !currentAPIsStack.isEmpty()) { try { - publisher.publishAPI(api); - } catch (java.lang.Exception e) { - log.error("Error occurred while publishing API '" + api.getId().getApiName(), e); + retryTime = retryTime * CONNECTION_RETRY_FACTOR; + Thread.sleep(retryTime); + } catch (InterruptedException te) { + log.error("Error occurred while sleeping", te); + } + if (!APIPublisherDataHolder.getInstance().getUnpublishedApis().isEmpty()) { + publishAPIs(currentAPIsStack, failedAPIsStack); + } else { + publishAPIs(failedAPIsStack, currentAPIsStack); } } } }); t.start(); } + + private void publishAPIs(Stack apis, Stack failedStack) { + while (!apis.isEmpty()) { + API api = apis.pop(); + try { + publisher.publishAPI(api); + } catch (Exception e) { + log.error("Error occurred while publishing API '" + api.getId().getApiName() + "'"); + failedStack.push(api); + } + } + } + } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java index af773d6df3..a88fe00e9f 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java @@ -55,6 +55,7 @@ public class AnnotationProcessor { private static final String PACKAGE_ORG_APACHE = "org.apache"; private static final String PACKAGE_ORG_CODEHAUS = "org.codehaus"; private static final String PACKAGE_ORG_SPRINGFRAMEWORK = "org.springframework"; + private static final String WILD_CARD = "/*"; private static final String AUTH_TYPE = "Any"; private static final String PROTOCOL_HTTP = "http"; @@ -227,7 +228,7 @@ public class AnnotationProcessor { if (methodContextAnno != null) { subCtx = invokeMethod(pathClazzMethods[0], methodContextAnno, STRING); } else { - subCtx = "/*"; + subCtx = WILD_CARD; } resource.setUriTemplate(makeContextURLReady(subCtx)); From 65612f724f07869774d64e2a10f186e6bba4b33d Mon Sep 17 00:00:00 2001 From: mharindu Date: Fri, 20 May 2016 16:02:56 +0530 Subject: [PATCH 2/2] Fixed compile issue --- .../java/org/wso2/carbon/device/mgt/jaxrs/api/Operation.java | 2 -- .../wso2/carbon/device/mgt/jaxrs/api/impl/OperationImpl.java | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Operation.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Operation.java index 759046bff8..01ebb45709 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Operation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Operation.java @@ -187,8 +187,6 @@ public interface Operation { @ApiResponse(code = 500, message = "Error occurred while fetching the activity for the supplied id.")}) @Permission(scope = "operation-view", permissions = {"/permission/admin/device-mgt/admin/devices/view"}) Response getActivity( - @ApiParam(name = "type", value = "Provide device {type} upon which the activity was performed", - required = true) @PathParam("type") String type, @ApiParam(name = "id", value = "Provide activity id {id} as ACTIVITY_(number)", required = true) @PathParam("id") String id) throws MDMAPIException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/OperationImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/OperationImpl.java index 2fb043bfd3..686b684456 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/OperationImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/OperationImpl.java @@ -235,13 +235,13 @@ public class OperationImpl implements org.wso2.carbon.device.mgt.jaxrs.api.Opera @Override @GET @Path("activity/{id}") - public Response getActivity(@PathParam("type") String type, @PathParam("id") String id) + public Response getActivity(@PathParam("id") String id) throws MDMAPIException { org.wso2.carbon.device.mgt.common.operation.mgt.Operation operation; DeviceManagementProviderService dmService; try { dmService = DeviceMgtAPIUtils.getDeviceManagementService(); - operation = dmService.getOperationByActivityId(type, id); + operation = dmService.getOperationByActivityId(id); } catch (OperationManagementException e) { String msg = "Error occurred while fetching the activity for the supplied id."; log.error(msg, e);