Java and JCA CAJ kazuro furukawa kek jp

  • Slides: 38
Download presentation
Java and JCA / CAJ < kazuro. furukawa @ kek. jp > Java and

Java and JCA / CAJ < kazuro. furukawa @ kek. jp > Java and JCA / CAJ Kazuro Furukawa <kazuro. furukawa@kek. jp> for EPICS 2009 at RRCAT January 29, 2009 Based on presentations by Kenneth Evans, Jr. , 2004 Kazuro Furukawa, 2006 EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 1

Java and JCA / CAJ Outline u. Java and EPICS u. Overview of JCA

Java and JCA / CAJ Outline u. Java and EPICS u. Overview of JCA u. Examples v. Simple. JCAGet v. Simple. JCAMonitor v. JProbe EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 2

Java and JCA / CAJ Java u. Java is designed to be platform independent

Java and JCA / CAJ Java u. Java is designed to be platform independent v. Write once, run everywhere u. Java programs are interpreted by another program, possibly on another machine v. The Java Virtual Machine (Java VM) u. Java technology includes v. J 2 SE v. J 2 EE v. J 2 ME Standard Edition Enterprise Edition (Multi-tier business apps) Micro Edition (Phones, PDAs, etc. ) u. Java is advertised to be all of these Simple Portable Interpreted Dynamic Architecture neutral Distributed Multithreaded Secure EPICS Workshop 2009, RRCAT, India Object oriented High performance Robust Kazuro Furukawa, KEK, Jan. 2009. 3

Java and JCA / CAJ Java and EPICS u. EPICS Channel Access is native

Java and JCA / CAJ Java and EPICS u. EPICS Channel Access is native code v. Not platform independent v. Build cycle is edit – compile [to machine code] - link - run u. Pure Java v. Build cycle is edit – compile [to byte code] – run [anywhere] u. Java Native Interface [JNI] is used to access native code v. Not “Pure Java” v. No longer platform independent v. You generate shared object or DLL libraries that must be used with the Java program u. You can write your own JNI v. Not that hard if you know Channel Access u. The EPICS build system handles Java projects and JNI u. Ant is an alternative EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 4

Java and JCA / CAJ JCA u. Stands for Java Channel Access u. JCA

Java and JCA / CAJ JCA u. Stands for Java Channel Access u. JCA is a JNI implementation of an EPICS Channel Access client library for Java v. Provides equivalents to most of the Channel Access API v. Developed by Eric Boucher while at the APS v. Currently taken over by Cosylab u. Available for download at vhttp: //jca. cosylab. com/ u. Latest version is available at Cosylab u. JCA Version 1 uses EPICS Base 3. 13 u. JCA Version 2 uses EPICS Base 3. 14 v. Channel Access is threaded v. Allows for preemptive callbacks ³Works better with Java, which is inherently threaded EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 5

Java and JCA / CAJ u. CAJ is a Java replacement for Channel Access

Java and JCA / CAJ u. CAJ is a Java replacement for Channel Access u. Developed at Cosylab (Control Systems Laboratory) v. Located in Ljubljana in Slovenia v. Cosylab also develops VDCT u. Available for download at vhttp: //caj. cosylab. com/ u. Latest version is available there u. Allows your programs to be “Pure Java” u. Is used with JCA v. Replaces JNI implementation v. Requires replacing only one line of code ³jca. create. Context(JCALibrary. JNI_THREAD_SAFE); ³jca. create. Context(“com. cosylab. epics. caj. CAJContext”); EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 6

Java and JCA / CAJ Requirements u. Java J 2 SE installed (Current/latest version

Java and JCA / CAJ Requirements u. Java J 2 SE installed (Current/latest version suggested) u. JCA v. Java libraries ³Download source and/or JAR files from the web v. Native JNI libraries ³Download from the web or build them ³Currently found with the 2. 1. 7 distribution wjca. dll wlibjca. so Windows Unix (Currently Linux, Solaris, Darwin? ) u. Your project v. JCA files need to be in your CLASSPATH v. UNIX: Shared object library needs to be in your LD_LIBRARY_PATH v. Windows: DLL needs to be in your PATH EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 7

Java and JCA / CAJ Resources u. EPICS web pages vhttp: //www. aps. anl.

Java and JCA / CAJ Resources u. EPICS web pages vhttp: //www. aps. anl. gov/epics/index. php v. Look under Extensions, then JCA u. JCA 2. 1. 7 API vhttp: //jca. cosylab. com/apidocs/index. html u. JCA 2. 1. 2 API vhttp: //www. aps. anl. gov/xfd/Soft. Dist/sw. BCDA/jca/2. 1. 2/api/index. html. I u. CAJ 1. 0. 5 vhttp: //caj. cosylab. com/manual. html u. Java Tutorial vhttp: //java. sun. com/learning/tutorial/index. html u. J 2 SE Documentation vhttp: //java. sun. com/reference/api/index. html u. J 2 SE 1. 4. 2 API (Javadoc) vhttp: //java. sun. com/j 2 se/1. 4. 2/docs/api/overview-summary. html EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 8

Java and JCA / CAJ JCA Packages u. Five Packages vgov. aps. jca Channel-Access-like

Java and JCA / CAJ JCA Packages u. Five Packages vgov. aps. jca Channel-Access-like routines vgov. aps. jca. configuration Configuration vgov. aps. jca. dbr DBR types vgov. aps. jca. event Event handling vgov. aps. jca. jni Native interface functions EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 9

Java and JCA / CAJ gov. aps. jca u. This is the package you

Java and JCA / CAJ gov. aps. jca u. This is the package you will use most directly u. Classes v. CASeverity v. CAStatus v. Channel. Connection. State v. Context Enum JCALibrary Monitor Valued. Enum u. Exceptions v. CAException EPICS Workshop 2009, RRCAT, India Timeout. Exception Kazuro Furukawa, KEK, Jan. 2009. 10

Java and JCA / CAJ JCALibrary u. Initializes JCALibrary jca=JCALibrary. get. Instance(); u. There

Java and JCA / CAJ JCALibrary u. Initializes JCALibrary jca=JCALibrary. get. Instance(); u. There is only one instance u. Used to create contexts and manage JCA configuration info u. Properties v. JNI_THREAD_SAFE preemptive ³Suggested for Java, which is inherently threaded v. JNI_SINGLE_THREADED non-preemptive u. Methods vcreate. Context vget. Property vlist. Properties vget. Version, get. Revision, get. Modification EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 11

Java and JCA / CAJ Context u. Corresponds to a Channel Access context u.

Java and JCA / CAJ Context u. Corresponds to a Channel Access context u. Created by JCALibrary. create. Context(JCALibrary. JNI_SINGLE_THREADED) create. Context(JCALibrary. JNI_THREAD_SAFE) u. Controls all IO u. You can have more than one context u. Methods vcreate. Channel vflush. IO, pend. Event, poll vattach. Current. Thread vadd. Context. Exception. Listener, remove. Context. Exception. Listener vadd. Context. Message. Listener, remove. Context. Message. Listener vdestroy EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 12

Java and JCA / CAJ Channel u. Represents a Channel Access channel u. Created

Java and JCA / CAJ Channel u. Represents a Channel Access channel u. Created by Context. create. Channel(String name, connection. Listener l) u. Properties v. CLOSED v. DISCONNECTED NEVER_CONNECTED u. Methods vget, many overloads vput, many overloads vget. Name, get. Connection. State, get. Element. Count, etc. vadd. Monitor vadd. Connection. Listener, remove. Connection. Listener vadd. Access. Rights. Listener, remove. Access. Rights. Listener vdestroy EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 13

Java and JCA / CAJ Monitor u. Represents a Channel Access monitor u. Created

Java and JCA / CAJ Monitor u. Represents a Channel Access monitor u. Created by Channel. add. Monitor(DBRType type, int count, int mask, Monitor. Listener l) u. Properties v. ALARM LOG VALUE u. Methods vadd. Monitor. Listener, remove. Monitor. Listener vget. Monitor. Listener, get. Monitor. Listeners vclear vget. Channel, get. Context vget. Count, get. Mask, get. Type vis. Monitoring. Alarm, is. Monitoring. Log, is. Monitoring. Value EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 14

Java and JCA / CAJ Monitor. Listener u. Part of gov. aps. jca. event

Java and JCA / CAJ Monitor. Listener u. Part of gov. aps. jca. event u. One method vmonitor. Changed u. Example private class My. Monitor. Listener implements Monitor. Listener { public void monitor. Changed(Monitor. Event ev) { // Call my handler on. Value. Changed(ev); } }; u. The value and status comes with the Monitor. Event EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 15

Java and JCA / CAJ Monitor. Event u. Part of gov. aps. jca. event

Java and JCA / CAJ Monitor. Event u. Part of gov. aps. jca. event u. Methods vget. DBR vget. Status How you get the value How you determine the status u. Example if(ev. get. Status() == CAStatus. NORMAL) { DBR dbr=ev. get. DBR(); double [] value=((DOUBLE)dbr). get. Double. Value(); } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 16

Java and JCA / CAJ Event Types u. Monitor. Listener u. Get. Listener u.

Java and JCA / CAJ Event Types u. Monitor. Listener u. Get. Listener u. Put. Listener u. Access. Rights. Listener u. Connection. Listener u. Context. Exception. Listener u. Context. Message. Listener Monitor. Event Get. Event Put. Event Access. Rights. Event Connection Event Context. Exception. Event Context. Message. Event u. Events all inherit from CAEvent u. They all work similarly to Monitor v. Call the routine that fires the event when it occurs v. Add a listener with the appropriate handler v. Get the data from the event that is passed to your handler EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 17

Java and JCA / CAJ gov. aps. jca. dbr u. Implements the EPICS DBR_xxx

Java and JCA / CAJ gov. aps. jca. dbr u. Implements the EPICS DBR_xxx types u. Interfaces v. DOUBLE, FLOAT, INT, STRING, TIME, CTRL, etc. u. Primary Class v. DBR u. Subclasses of DBR v. DBR_Double, DBR_Float, DBR_Int, DBR_STS_Double, etc. u. Example: DBR_STS_Double v. Interfaces ³STS, DOUBLE v. Extends ³DBR_Double v. Subclasses ³DBR_GR_Double, DBR_Time_Double EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 18

Java and JCA / CAJ Simple. JCAGet package simplejca; import gov. aps. jca. *;

Java and JCA / CAJ Simple. JCAGet package simplejca; import gov. aps. jca. *; import gov. aps. jca. dbr. *; EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 19

Java and JCA / CAJ Simple. JCAGet public class Simple. JCAGet { public static

Java and JCA / CAJ Simple. JCAGet public class Simple. JCAGet { public static void main(String[] args) { Simple. JCAGet simple. JCAGet = new Simple. JCAGet(); JCALibrary jca=null; Context ctxt=null; Channel chan=null; // Parse the command line if(!simple. JCAGet. parse. Command(args)) System. exit(1); if(!simple. JCAGet. pv. Specified) { System. err. println("No PV specifiedn"); System. exit(1); } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 20

Java and JCA / CAJ Simple. JCAGet // Initialize and search try { //

Java and JCA / CAJ Simple. JCAGet // Initialize and search try { // Get the JCALibrary instance jca=JCALibrary. get. Instance(); // Create a non-preemptive context=jca. create. Context( JCALibrary. JNI_SINGLE_THREADED); // Search chan=ctxt. create. Channel(simple. JCAGet. name); // Wait for search ctxt. pend. IO(simple. JCAGet. timeout); } catch(Exception ex) { System. err. println("Search failed for " + simple. JCAGet. name + ": n" + ex); System. exit(1); } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 21

Java and JCA / CAJ Simple. JCAGet // Get the first value as a

Java and JCA / CAJ Simple. JCAGet // Get the first value as a String try { // Get the value String [] value; value=((STRING)chan. get(DBRType. STRING, 1)). get. String. Value(); // Wait for the get ctxt. pend. IO(simple. JCAGet. timeout); // Print the value System. out. println("The value of " + simple. JCAGet. name + " is " + value[0]); } catch(Exception ex) { System. err. println("Get failed for " + simple. JCAGet. name + ": n" + ex); System. exit(1); } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 22

Java and JCA / CAJ Simple. JCAGet // Clean up try { // Clear

Java and JCA / CAJ Simple. JCAGet // Clean up try { // Clear the channel chan. destroy(); // Destroy the context ctxt. destroy(); } catch(Exception ex) { System. err. println("Clean up failed for " + simple. JCAGet. name + ": n" + ex); System. exit(1); } // Successful exit System. exit(0); } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 23

Java and JCA / CAJ Simple. JCAGet output java. exe -classpath <simplejca-path>Simple. JCA. jar;

Java and JCA / CAJ Simple. JCAGet output java. exe -classpath <simplejca-path>Simple. JCA. jar; <jca-path>jca-2. 1. 7. jar simplejca. Simple. JCAGet evans: calc The value of evans: calc is 3 EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 24

Java and JCA / CAJ Simple. JCAMonitor u. Similar to Simple. JCAGet v. Imports,

Java and JCA / CAJ Simple. JCAMonitor u. Similar to Simple. JCAGet v. Imports, parsing the command line, etc. are the same u. We will have listeners u. We will use JNI_THREAD_SAFE (preemptive) u. We will use flush. IO and not pend. IO, etc. EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 25

Java and JCA / CAJ Simple. JCAMonitor /** Implementation of Connection Listener class */

Java and JCA / CAJ Simple. JCAMonitor /** Implementation of Connection Listener class */ private class SJCAConnection. Listener implements Connection. Listener { public void connection. Changed(Connection. Event ev) { on. Connection. Changed(ev); } }; /** Implementation of Monitor. Listener class */ private class SJCAMonitor. Listener implements Monitor. Listener { public void monitor. Changed(Monitor. Event ev) { on. Value. Changed(ev); } }; EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 26

Java and JCA / CAJ Simple. JCAMonitor // Instance of Simple. JCAMonitor sjcam=new Simple.

Java and JCA / CAJ Simple. JCAMonitor // Instance of Simple. JCAMonitor sjcam=new Simple. JCAMonitor(); // Initialize JCA try { // Get the JCALibrary instance jca=JCALibrary. get. Instance(); // Create a preemptive context, default configuration ctxt=jca. create. Context(JCALibrary. JNI_THREAD_SAFE); } catch(Exception ex) { System. err. println("Initialization failed for " + sjcam. name + ": n" + ex); System. exit(1); } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 27

Java and JCA / CAJ Simple. JCAMonitor // Search try { // Search chan=ctxt.

Java and JCA / CAJ Simple. JCAMonitor // Search try { // Search chan=ctxt. create. Channel(sjcam. name, sjcam. new SJCAConnection. Listener()); ctxt. flush. IO(); } catch(Exception ex) { System. err. println("Search failed for " + sjcam. name + ": n" + ex); System. exit(1); } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 28

Java and JCA / CAJ Simple. JCAMonitor private void on. Connection. Changed(Connection. Event ev)

Java and JCA / CAJ Simple. JCAMonitor private void on. Connection. Changed(Connection. Event ev) { Channel ch=(Channel)ev. get. Source(); Context ctxt=ch. get. Context(); // Start a monitor on the first connection if(connection. Counter == 0 && ch. get. Connection. State() == Channel. CONNECTED) { try { // Add a monitor listener and flush ch. add. Monitor(DBRType. STRING, 1, Monitor. VALUE|Monitor. LOG|Monitor. ALARM, new SJCAMonitor. Listener()); ctxt. flush. IO(); } catch(Exception ex) { ex. print. Stack. Trace(); } } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 29

Java and JCA / CAJ Simple. JCAMonitor // Print connection state if(ch. get. Connection.

Java and JCA / CAJ Simple. JCAMonitor // Print connection state if(ch. get. Connection. State() == Channel. CONNECTED) { System. out. println(ch. get. Name() + " is connected"); } else if(ch. get. Connection. State() == Channel. CLOSED) { System. out. println(ch. get. Name() + " is closed"); } else if(ch. get. Connection. State() == Channel. DISCONNECTED) { System. out. println(ch. get. Name() + " is disconnected"); } else if(ch. get. Connection. State() == Channel. NEVER_CONNECTED) { System. out. println(ch. get. Name() + " is never connected"); } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 30

Java and JCA / CAJ Simple. JCAMonitor private void on. Value. Changed(Monitor. Event ev)

Java and JCA / CAJ Simple. JCAMonitor private void on. Value. Changed(Monitor. Event ev) { Channel ch=(Channel)ev. get. Source(); Context ctxt=ch. get. Context(); // Check the status if (ev. get. Status() != CAStatus. NORMAL) { System. err. println("monitor. Changed: Bad status”); } // Get the value from the DBR try { DBR dbr=ev. get. DBR(); String [] value=((STRING)dbr). get. String. Value(); System. out. print(SJCAUtils. time. Stamp() + " " + get. Name() + ": “ + value[0]); } catch(Exception ex) {. . . } } EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 31

Java and JCA / CAJ Simple JCAMonitor output Oct 11, 2004 10: 36: 43.

Java and JCA / CAJ Simple JCAMonitor output Oct 11, 2004 10: 36: 43. 661 Starting Simple JCA Monitor Oct 11, 2004 10: 36: 44. 083 Search successful for: evans: calc CHANNEL : evans: calc TYPE : gov. aps. jca. dbr. DBRType[DBR_DOUBLE=6] COUNT : 1 STATE : gov. aps. jca. Channel$Connection. State[CONNECTED=2] HOSTNAME : ctlapps 4 l 188. aps 4. anl. gov: 5064 READ : true WRITE : true Oct 11, 2004 10: 36: 44. 208 evans: calc is connected Oct 11, 2004 10: 36: 44. 224 evans: calc: 2 Oct 11, 2004 10: 36: 44. 224 evans: calc: 3. . . Oct 11, 2004 10: 36: 53. 240 evans: calc: 3 Oct 11, 2004 10: 36: 53. 740 evans: calc: 4 Oct 11, 2004 10: 36: 54. 036 All Done EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 32

Java and JCA / CAJ JProbe u. JProbe is a simple example that demonstrates

Java and JCA / CAJ JProbe u. JProbe is a simple example that demonstrates using JCA in a Swing GUI EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 33

Java and JCA / CAJ Write once, run everywhere? EPICS Workshop 2009, RRCAT, India

Java and JCA / CAJ Write once, run everywhere? EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 34

Java and JCA / CAJ Source files for Simple JCA routines u. All the

Java and JCA / CAJ Source files for Simple JCA routines u. All the source and JAR files should be available with the presentation v. LICENSE v. Simple. JCA. jar vsimplejca ³Simple. JCAMonitor. java ³Simple. JCAGet. java ³SJCAUtils. java v. JProbe. jar vjprobe ³JProbe. java ³Main. Frame. java ³About. Box. Panel. java u. Stored as Simple. JCA. zip EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 35

Java and JCA / CAJ Some Pointers to Documents u. Example files vhttp: //www.

Java and JCA / CAJ Some Pointers to Documents u. Example files vhttp: //www. aps. anl. gov/epics/ v. Documents - Training - Developing Client Tools - Java and JCA - Example Files u. Build examples of EPICS-Base, etc on several Platforms vhttp: //www-linac. kek. jp/jk/win 32/ vhttp: //www-linac. kek. jp/jk/linux/ vhttp: //www-linac. kek. jp/jk/darwin/ EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 36

Java and JCA / CAJ Acknowledgements u. JCA was developed by Eric Boucher while

Java and JCA / CAJ Acknowledgements u. JCA was developed by Eric Boucher while at the APS u. Matej Sekoranja [Cosylab] has taken over JCA and is developing CAJ u. Both of these people were very helpful in getting JCA working for me EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 37

Java and JCA / CAJ Thank You EPICS Workshop 2009, RRCAT, India Kazuro Furukawa,

Java and JCA / CAJ Thank You EPICS Workshop 2009, RRCAT, India Kazuro Furukawa, KEK, Jan. 2009. 38