Tuesday Mar 10, 2009

Cup of poJO anyone?

NOTE, the instructions here have been updated a little for GlassFish ESB v2.1 since its release.

A lot of the demos until this point have illustrated Scheduler BC consuming a BPEL SE, so how 'bout consuming a 'cup of Jo' instead?  POJO that is. 

We'll now show how to have Scheduler BC trigger a POJO SE to execute a Windows (2000, XP, Vista) command that pops up a dialog with some greeting.  Of course, you can adapt this to do more useful things like 'downloading a file from some remote site so that File BC can pick it up and send it to BPEL SE for processing'...but then that'll be work and fun is good!

Prerequisites: Scheduler BC now comes pre-installed with GF ESB v2.1, but you'll need to install POJO SE (installer here).

  1. Create a Java (SE) Application project, keeping the default Main class, although it won't be used here (but it's needed due to a POJO SE project idiosyncrasy):

  2. Right-click the schedpojodemo package and add a New | Other | ESB | POJO for Binding:

  3. Specify that the POJO SE is to work with Scheduler BC by:

  4. Select Next and Add a Simple Trigger:

    The Windows command chosen here in the trigger Message is long so you can copy it from here:

  5. Select Next and Choose Node for the Input Argument Type in the POJO method that will be doing the providing:

  6. Select Finish and in the SchedPojoBinding.java Java editor that shows up, fix the package import warning for Node (answer: org.w3c.dom.Node):

  7. Copy the imports below into the same class, pasting it before the first import statement that's already there:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Level;
                
  8. Copy the code below into the same class, pasting it just above the line that declares "private static final Logger logger":
        private void FireTriggerOperation(String input) {
            List args = new ArrayList();
            if (System.getProperty("os.name").toLowerCase()                 //NOI18N
                    .startsWith("windows")) {                               //NOI18N
                args.add("cmd.exe");                                        //NOI18N
                args.add("/c");                                             //NOI18N
            }
            args.add(input);
            ProcessBuilder procBuilder = new ProcessBuilder(args);
            try {
                Process proc = procBuilder.start();
                StreamEater stdOutEater =
                        new StreamEater(proc.getInputStream(), "StdOut");   //NOI18N
                StreamEater stdErrEater =
                        new StreamEater(proc.getErrorStream(), "StdErr");   //NOI18N
                stdOutEater.start();
                stdErrEater.start();
                int rc = proc.waitFor();
                logger.info("Command \\"" + input + "\\" exit code: " + rc);  //NOI18N
            } catch (IOException ex) {
                logger.log(Level.SEVERE, "Unknown command? " + input, ex);  //NOI18N
                return;
            } catch (InterruptedException ie) {
                logger.log(Level.WARNING, "Command aborted!");              //NOI18N
            }
        }
    
        private class StreamEater extends Thread {
    
            private BufferedReader rdr;
            private String name;
    
            public StreamEater(InputStream is, String name) {
                super(name + "-Eater");                                     //NOI18N
                rdr = new BufferedReader(new InputStreamReader(is));
                this.name = name;
            }
    
            @Override
            public void run() {
                String line = null;
                try {
                    while ((line = rdr.readLine()) != null) {
                        logger.info(name + "> " + line);                    //NOI18N
                    }
                } catch (IOException ex) {
                    logger.log(Level.SEVERE, "Problem reading " + name, ex);//NOI18N
                }
                try {
                    rdr.close();
                } catch (IOException ex) {
                    // ignore
                }
                rdr = null;
            }
        }
    
        public static void main(String[] args) throws Throwable {
            SchedPojoBinding spb = new SchedPojoBinding();
            spb.FireTriggerOperation("echo MsgBox \\"Howdy, all's well!\\", " //NOI18N
                    + "0, \\"Scheduler POJO Demo\\" > C:\\\\temp\\\\howdy.vbs & " //NOI18N
                    + "wscript.exe C:\\\\temp\\\\howdy.vbs");                   //NOI18N
        }
    
  9. In the FireTriggerOperation(Node inupt) method, add this call:

  10. From here on, it's pretty much mundane...Save this project and create a new Composite Application project
  11. Drag the SchedPojoDemo project into the Service Assembly canvas and do a Build Project
  12. Deploy the CASA project and voila, you got your popup!

About

Edward Wong

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today