java - Plugin framework and bi-directional program communication -
what way let 2 different separate java programs communicate each other?
i not sure if want use osgi (but seems 1 still gets updates; jpf , jspf old). new topic plugin framework. right osgi description of architecture, plugin structure etc. , equinox , other thing real implementation, have use?
i make things more precise: want program core tool should able hot-load plugins, , bidirectionally communicate them , javafx gui.
i use class controlling gui , additional class algorithms , on (mvc). think style no longer helpful structure plugin-based tool.
i want use design pattern beginning, else it'll end mess.
the simplest solution problem use serviceloader
(doc here). included in java, , simple use:
- load jar file(s) @ runtime
- detect classes implement particular interface (e.g.:
your.package.yourservice
). - instantiate objects these classes.
here pretty post describing how (note: should use second proposal urlclassloader
; not extend classpath dynamically). also, not forget declare services inside jar's meta-inf directory:
if
com.example.impl.standardcodecs
implementation ofcodecset
service jar file contains file namedmeta-inf/services/com.example.codecset
this file contains single line:
com.example.impl.standardcodecs # standard codecs
by choosing approach, core program naturally have handle plugins, able communicate them easily. ensure bi-directional communication (i.e. plugins calling core program), suggest create interface core program implement, , pass interface plugins.
plugin interface:
public interface plugin { public void dopluginstuff(caller caller); }
core program caller
interface:
public interface caller { public void sendbackresults(object results); }
plugin implementation (in separate jar file):
public class aparticularplugin implements plugin { public void dopluginstuff(caller caller){ caller.sendbackresults("hello world"); } }
core program:
public class coreprogram implements caller { public void callplugin(url[] urltojarfiles){ urlclassloader ucl = new urlclassloader(urltojarfiles); serviceloader<plugin> sl = serviceloader.load(plugin.class, ucl); iterator<plugin> plugins = sl.iterator(); while (plugins.hasnext()) plugins.next().dopluginstuff(this); } public void sendbackresults(object results){ system.out.println(results.tostring()); } }
Comments
Post a Comment