|
| 1 | +package com.openfin.desktop.demo; |
| 2 | + |
| 3 | +import com.openfin.desktop.*; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | + |
| 7 | +import java.util.concurrent.CountDownLatch; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 10 | +import java.util.concurrent.atomic.AtomicInteger; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * This class can be used to profile memory usage of Java Adapter for communicating with Runtime. The initial version |
| 15 | + * just keeps calling getMachineId API. It can easily extended to test other APIs. |
| 16 | + * |
| 17 | + * VisualVM, available from https://visualvm.github.io/, can be used to monitor memory usgage while this code is running |
| 18 | + */ |
| 19 | + |
| 20 | +public class MemoryProfile { |
| 21 | + private final static Logger logger = LoggerFactory.getLogger(MemoryProfile.class.getName()); |
| 22 | + |
| 23 | + public static void main(String[] args) { |
| 24 | + RuntimeConfiguration runtimeConfiguration = new RuntimeConfiguration(); |
| 25 | + String connectionUuid = MemoryProfile.class.getName(); |
| 26 | + String desktopVersion = java.lang.System.getProperty("com.openfin.demo.version"); |
| 27 | + if (desktopVersion == null) { |
| 28 | + desktopVersion = "stable"; |
| 29 | + } |
| 30 | + runtimeConfiguration.setRuntimeVersion(desktopVersion); |
| 31 | + try { |
| 32 | + final DesktopConnection desktopConnection = new DesktopConnection(connectionUuid); |
| 33 | + DesktopStateListener listener = new DesktopStateListener() { |
| 34 | + @Override |
| 35 | + public void onReady() { |
| 36 | + launchThread(desktopConnection); |
| 37 | + } |
| 38 | + @Override |
| 39 | + public void onClose(String error) { |
| 40 | + |
| 41 | + } |
| 42 | + @Override |
| 43 | + public void onError(String reason) { |
| 44 | + logger.error(String.format("onError %s", reason)); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onMessage(String message) { |
| 49 | + } |
| 50 | + @Override |
| 51 | + public void onOutgoingMessage(String message) { |
| 52 | + } |
| 53 | + }; |
| 54 | + desktopConnection.connect(runtimeConfiguration, listener, 50); |
| 55 | + } catch (Exception e) { |
| 56 | + logger.error("", e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static void launchThread(DesktopConnection desktopConnection) { |
| 61 | + Thread t = new Thread() { |
| 62 | + @Override |
| 63 | + public void run() { |
| 64 | + OpenFinRuntime openfinSystem = new OpenFinRuntime(desktopConnection); |
| 65 | + AtomicInteger callCount = new AtomicInteger(); |
| 66 | + AtomicBoolean shouldRun = new AtomicBoolean(true); |
| 67 | + while (shouldRun.get()) { |
| 68 | + try { |
| 69 | + CountDownLatch latch = new CountDownLatch(1); |
| 70 | + openfinSystem.getMachineId(new AckListener() { |
| 71 | + @Override |
| 72 | + public void onSuccess(Ack ack) { |
| 73 | + if (ack.isSuccessful()) { |
| 74 | + logger.info(String.format("API call count %d", callCount.incrementAndGet())); |
| 75 | + latch.countDown(); |
| 76 | + } else { |
| 77 | + logger.error(String.format("API failed %s", ack.getReason())); |
| 78 | + shouldRun.set(false); |
| 79 | + } |
| 80 | + } |
| 81 | + @Override |
| 82 | + public void onError(Ack ack) { |
| 83 | + logger.error(String.format("API failed %s", ack.getReason())); |
| 84 | + shouldRun.set(false); |
| 85 | + } |
| 86 | + }); |
| 87 | + latch.await(1, TimeUnit.SECONDS); |
| 88 | + } catch (Exception ex) { |
| 89 | + ex.printStackTrace(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }; |
| 94 | + t.start(); |
| 95 | + } |
| 96 | +} |
0 commit comments