|
| 1 | +package io.harness.ff.examples; |
| 2 | + |
| 3 | +import io.harness.cf.client.api.BaseConfig; |
| 4 | +import io.harness.cf.client.api.CfClient; |
| 5 | +import io.harness.cf.client.api.Event; |
| 6 | +import io.harness.cf.client.api.XmlFileMapStore; |
| 7 | +import io.harness.cf.client.connector.HarnessConfig; |
| 8 | +import io.harness.cf.client.connector.HarnessConnector; |
| 9 | +import io.harness.cf.client.dto.Target; |
| 10 | +import io.harness.cf.model.FeatureSnapshot; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.concurrent.ScheduledExecutorService; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | + |
| 18 | +@Slf4j |
| 19 | +public class EventExampleWithFeatureSnapshot { |
| 20 | + private static final String SDK_KEY = ""; |
| 21 | + private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); |
| 22 | + |
| 23 | + private static CfClient client; |
| 24 | + |
| 25 | + public static void main(String... args) { |
| 26 | + |
| 27 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 28 | + scheduler.shutdown(); |
| 29 | + client.close(); |
| 30 | + })); |
| 31 | + |
| 32 | + final XmlFileMapStore fileStore = new XmlFileMapStore("Non-Freemium"); |
| 33 | + // this is one way of initialising config. |
| 34 | + final HarnessConnector hc = new HarnessConnector(SDK_KEY, HarnessConfig.builder().build()); |
| 35 | + |
| 36 | + BaseConfig bc = BaseConfig.builder(). |
| 37 | + enableFeatureSnapshot(true).build(); |
| 38 | + |
| 39 | + // initialise the client. |
| 40 | + client = new CfClient(hc, bc); |
| 41 | + |
| 42 | + client.on(Event.READY, result -> log.info("READY")); |
| 43 | + |
| 44 | + // example: specified prefix we can filter on. |
| 45 | + final String FLAG_PREFIX = "FFM_"; |
| 46 | + // example: given flag change event - get both previous and current feature if prefix is matched. |
| 47 | + client.on(Event.CHANGED, identifier -> { |
| 48 | + if (identifier.startsWith(FLAG_PREFIX)) { |
| 49 | + getSnapshot(identifier); |
| 50 | + } else { |
| 51 | + log.info("We had an event change but flag did not have required prefix"); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + // example : given flag change event - get all snapshots. |
| 56 | + client.on(Event.CHANGED, identifier -> { |
| 57 | + getAllSnapshots(); |
| 58 | + }); |
| 59 | + |
| 60 | + // example : given flag change event - get all snapshots with given prefix. |
| 61 | + client.on(Event.CHANGED, identifier -> { |
| 62 | + getAllSnapshotsWithPrefix(); |
| 63 | + }); |
| 64 | + |
| 65 | + final Target target = Target.builder().identifier("target1").attribute("testKey", "TestValue").name("target1").build(); |
| 66 | + |
| 67 | + scheduler.scheduleAtFixedRate(() -> { |
| 68 | + log.info("ticking..."); |
| 69 | + }, 0, 10, TimeUnit.SECONDS); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + // example method to extract a single snapshot. |
| 75 | + private static void getSnapshot(String identifier) { |
| 76 | + log.info("We had a chang event and prefix matched, lets inspect the diff"); |
| 77 | + // fetch current and previous version of the feature |
| 78 | + FeatureSnapshot snapshot = client.getFeatureSnapshot(identifier); |
| 79 | + log.info("Previous flag config: {}, {}", identifier, snapshot.getPrevious()); |
| 80 | + log.info("Current flag config: {}, {}", identifier, snapshot.getCurrent()); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + // example method to extract and print all the snapshots. |
| 85 | + private static void getAllSnapshots() { |
| 86 | + // get all snapshots |
| 87 | + List<FeatureSnapshot> snapshots = client.getAllFeatureSnapshots(); |
| 88 | + int counter = 0; |
| 89 | + for (FeatureSnapshot snapshot : snapshots) { |
| 90 | + log.info("snapshots {} {}", ++counter, snapshot); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // example method to extract and print all the snapshots. |
| 95 | + private static void getAllSnapshotsWithPrefix() { |
| 96 | + // get all snapshots |
| 97 | + String prefix = "FFM_"; |
| 98 | + List<FeatureSnapshot> snapshots = client.getAllFeatureSnapshots(prefix); |
| 99 | + int counter = 0; |
| 100 | + for (FeatureSnapshot snapshot : snapshots) { |
| 101 | + log.info("snapshots {} {}", ++counter, snapshot); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | + |
0 commit comments