Class Channel<Data extends Channel.Config>

java.lang.Object
org.apidesign.jvm.channel.Channel<Data>
Type Parameters:
Data - internal data of the channel
All Implemented Interfaces:
AutoCloseable

public final class Channel<Data extends Channel.Config> extends Object implements AutoCloseable
Channel connects two JVM instances. A "channel" creates two (almost) identical instances of the Channel on both sides of the "channel" - e.g. in each of the JVMs. The instances are initialized with the same Channel.Config, so they both understand the same messages when communicating with each other.
  • Method Details

    • create

      public static <D extends Channel.Config> Channel<D> create(JVM jvm, Class<? extends D> configClass)
      Factory method to initialize the Channel in the SubstrateVM.
      Type Parameters:
      D - type of internal data as well as provider of the pool
      Parameters:
      jvm - instance of HotSpot JVM to connect to (can be null to create a mock channel inside of a single JVM)
      configClass - the class which has public default constructor and can supply an instance of persistance pool to use for communication
      Returns:
      channel for sending messages to the HotSpot JVM
    • getConfig

      public final Data getConfig()
      Getter for data associated with the channel. Each instance of Channel on both sides of the "channel" gets different instance of Data. The data may be used in the functions that implement the logic in execute(Class, Function) message processing.
      Returns:
      data associated with this channel
      See Also:
    • isMaster

      public final boolean isMaster()
      Master channel check. One instance of the Channel on the initializing side is marked as master. The other one is slave.
      Returns:
      is master
    • isDualJvmMode

      public boolean isDualJvmMode()
      Check for "real dual JVM mode". There is a way to create a channel in a "mock mode" via create(org.apidesign.ni.jvm.channel.JVM, java.lang.Class) - such emulation is running both sides of the channel in the same JVM. One can detect such a situation by checking result of this method.
      Returns:
      true when two different JVMs are on each side of the channel. false otherwise, for example in the "mock mode".
    • execute

      public final <C, R extends C> R execute(Class<C> resultType, Function<? super Channel<Data>, R> msg)
      Executes a message in the other JVM. The message is any subclass of Function ready to be serialized/deserialized via channel's Channel.Config. The result (which is of type R) also has to be registered for serialization/deserialization.
      Type Parameters:
      C - the actual type of the Channel.Config subclass
      R - the type of result we expect the message to return
      Parameters:
      resultType - class with the type of R to use for deserialization
      msg - the message that gets serialized, transferred into the other JVM, deserialized on the other side and evaluated there
      Returns:
      the value gets computed via Function.apply(T) of msg in the other JVM and then it gets serialized and transferred back to us. Deserialized and the value is then returned from this method
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception
    • toString

      public String toString()
      Overrides:
      toString in class Object