Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Provisioning looping issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
davetobin committed Jul 7, 2016
1 parent 4f07c13 commit 543a7f9
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public synchronized boolean updateCurrentWeight(String url, Rest method){
logger.finest("currentWeight = " + this.currentWeight);
this.currentWeight = this.currentWeight + getWeightValuePerBSSCall(url, method);
logger.finest("currentWeight updated = " + this.currentWeight);
logger.finest("Threshold = " + threshold);
if( currentWeight >= threshold ){
callPermitted = false ;
logger.warning("THRESHOLD REACHED OR EXCEEDED !!!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* © Copyright IBM Corp. 2014
*
* 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 com.ibm.sbt.provisioning.sample.app.services;

import com.ibm.sbt.provisioning.sample.app.task.BSSProvisioning;
/**
* This class represents a singleton that encapsulate an instance of
* the class <code>com.ibm.sbt.services.client.smartcloud.bss.GRTESubscriberManagementService</code>
* */


//TODO Temp class Subscriber can be used when rest of code moved to SocialSDK
public class GRTESubscriber {

private static GRTESubscriber instance = null;
private GRTESubscriberManagementService service;

private GRTESubscriber() {
this.service = new GRTESubscriberManagementService(BSSProvisioning.getBasicEndpoint());
}

public static synchronized GRTESubscriber getInstance() {
if (instance == null)
instance = new GRTESubscriber();
return instance;
}

public GRTESubscriberManagementService getService() {
return service;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.ibm.sbt.provisioning.sample.app.services;

import java.io.IOException;

import com.ibm.commons.util.io.json.JsonException;
import com.ibm.commons.util.io.json.JsonJavaFactory;
import com.ibm.commons.util.io.json.JsonJavaObject;
import com.ibm.commons.util.io.json.JsonParser;
import com.ibm.sbt.services.client.ClientService;
import com.ibm.sbt.services.client.Response;
import com.ibm.sbt.services.client.smartcloud.bss.BssException;
import com.ibm.sbt.services.client.smartcloud.bss.SubscriberJsonBuilder;
import com.ibm.sbt.services.client.smartcloud.bss.SubscriberManagementService;
import com.ibm.sbt.services.endpoints.Endpoint;

// TODO Move to SubscriberManagementService when code can be merged to SocialSDK


public class GRTESubscriberManagementService extends
SubscriberManagementService {

/**
* Constructor
*
* @param endpointName
*/
public GRTESubscriberManagementService(Endpoint endpoint) {
super(endpoint);
}

private static final long serialVersionUID = 2L;

/**
* Add a subscriber w/ suppressing emails either to the vendor's organization or to the organization of one of the vendor's customers.
*
* @param subscriber
* @return {JsonJavaObject}
* @throws BssException
* @throws JsonException
* @throws IOException
*/
public JsonJavaObject addSubscriberSuppressEmail(SubscriberJsonBuilder subscriber) throws BssException, IOException, JsonException {
return addSubscriberSuppressEmail(subscriber.toJson());
}

/**
* Add a subscriber w/ suppressing emails either to the vendor's organization or to the organization of one of the vendor's customers.
*
* @param subscriberJson
* @return {JsonJavaObject}
* @throws BssException
* @throws JsonException
* @throws IOException
*/
public JsonJavaObject addSubscriberSuppressEmail(String subscriberJson) throws BssException, JsonException, IOException {
JsonJavaObject jsonObject = (JsonJavaObject)JsonParser.fromJson(JsonJavaFactory.instanceEx, subscriberJson);
return addSubscriberSuppressEmail(jsonObject);
}

/**
* Add a subscriber w/ suppressing emails either to the vendor's organization or to the organization of one of the vendor's customers.
*
* @param subscriberJson
* @return JSON object containing
* @throws BssException
* @throws IOException
*/
public JsonJavaObject addSubscriberSuppressEmail(JsonJavaObject subscriberJson) throws BssException {
try {
// TODO add url to BSSUrls when moving to SocialSDK
String serviceUrl = "/api/bss/resource/subscriber?suppressEmail=true";
Response serverResponse = createData(serviceUrl, null, JsonHeader, subscriberJson, ClientService.FORMAT_JSON);
return (serverResponse == null) ? null : (JsonJavaObject)serverResponse.getData();
} catch (Exception e) {
throw new BssException(e, "Error adding subscriber {0} caused by {1}", subscriberJson, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -115,6 +116,14 @@ public class BSSProvisioning implements Runnable {
* number of currently provisioned subscribers
*/
protected static AtomicInteger subscribersProvisioned = new AtomicInteger(0);
/**
* number of currently failed subscribers
*/
protected static AtomicInteger subscribersFailed = new AtomicInteger(0);
/**
* list of failed subscribers
*/
protected static List<SubscriberTask> failedSubscriptions = new ArrayList<SubscriberTask>();
/**
* <code>Map</code> associating each subscribers with a String array, containing a timestamp for each
* state transition of the corresponding subscriber
Expand Down Expand Up @@ -302,7 +311,7 @@ public static void main( String[] args ){
.getAsString("EmailAddress");
SubscriberTask subscriberTask =
new SubscriberTask(subscriber, customerId, subscriptionId,
SubscriberTask.State.SUBSCRIBER_NON_EXISTENT );
SubscriberTask.State.SUBSCRIBER_NON_EXISTENT, 10);
subscriberTasks.add(subscriberTask);
stateTransitionReport.put(suscriberEmail, new String[6]);
subscriberWeightReport.put(suscriberEmail, new int[3]);
Expand Down Expand Up @@ -376,7 +385,7 @@ public void run(){
Thread.currentThread().setName("Provisioning");
}

if(BSSProvisioning.getSubscribersQuantity().get() > BSSProvisioning.getSubscribersProvisioned().get()){
if(BSSProvisioning.getSubscribersQuantity().get() > (BSSProvisioning.getSubscribersProvisioned().get() + BSSProvisioning.getSubscribersFailed().get())){
// stop submitting when the threshold has been reached and the queue has been emptied
while( !WeightManager.getInstance().isThresholdReached() && subscriberTasks != null && !subscriberTasks.isEmpty() ){
SubscriberTask subscriberTask = subscriberTasks.poll();
Expand All @@ -387,11 +396,24 @@ public void run(){
subscriberReport[0] = sdf.format(new Date());
}
threadpool.submit(subscriberTask);

if(subscriberTask.getStatus() == SubscriberTask.State.SUBSCRIBE_FAILED){
logger.info("Subscriber task for " +subscriberTask.getSubscriberId()+ " failed due to exceeded attempts");
}
}
//sleeping 250 millisec between one submit and the other with the goal of
//avoiding to overwhelm the server with requests
Thread.sleep(250);
}
}else{
if(BSSProvisioning.getSubscribersFailed().get()>0){
logger.warning("Provisioned Subscribers: "+ BSSProvisioning.getSubscribersProvisioned().get() +"\nFailed Subscribers: "+ BSSProvisioning.getSubscribersFailed().get());

}else{
logger.finest("ALL SUBSCRIBERS PROVISIONED !!!");
}
logger.info("Provisioning finished");
WeightManager.getInstance().shutdown();
}
}catch(Exception e){
logger.severe("Catched Exception in the BSSProvisioning run() method : "+e.getMessage() );
Expand Down Expand Up @@ -742,9 +764,30 @@ public static synchronized void incrementSubscribersProvisioned(){
/**
* {@link #subscribersProvisioned} getter method
*/
public static synchronized AtomicInteger getSubscribersProvisioned(){
public static synchronized AtomicInteger getSubscribersProvisioned(){
return subscribersProvisioned ;
}

/**
* {@link #subscribersFailed} incrementation method
*/
public static synchronized void incrementSubscribersFailed(){
subscribersFailed.incrementAndGet();
}

/**
* {@link #subscribersFailed} getter method
*/
public static synchronized AtomicInteger getSubscribersFailed(){
return subscribersFailed ;
}

/**
* {@link #failedSubscriptions} getter method
*/
public static synchronized List<SubscriberTask> getFailedSubscriptions(){
return failedSubscriptions ;
}

/**
* {@link #stateTransitionReport} getter method
Expand Down
Loading

0 comments on commit 543a7f9

Please sign in to comment.