TOTD #23: JavaFX Client invoking a Metro endpoint

This TOTD is inspired by Learning JavaFX Script - Part 3. The original article explains how to invoke a Web service from a JavaFX client using NetBeans 5.5.1 and GlassFish v1. Newer version of both NetBeans and GlassFish are available since the article was written. This TOTD (tip of the day) explains how to invoke a Metro endpoint deployed on GlassFish v2 from a JavaFX client - all using NetBeans 6.

  1. Following screencast #ws7, create a plain (without Security or Reliability enabled) Metro endpoint using NetBeans 6 and GlassFish v2.
  2. In NetBeans 6 IDE, install the JavaFX plugin as described here.
  3. Create Web service client library - Creating a Web service client in JavaFX Script Application is causing a NullPointerException (issue #126352). The workaround I used is to create a separate library with client-side artifacts and then include it as dependency in the JavaFX client project.
    1. Create a new project of type "Java Class Library" as shown below:



      and click on "Finish".
    2. Enter the project name as "MetroClientLibrary" as shown below:



      and click on "Finish".
    3. Right-click on the newly created project, select "New", "Web Service Client...".
    4. Click on "Browse..." button next to "Project" radio button and select the deployed Web service from Metro endpoint project. If the Web service is deployed on a different machine then you may specify the WSDL URL. Specify the package name "client" as shown below:



      and click on "Finish".
    5. Once the Web service client-side artifacts are generated (indicated by expandable Web Service References tree node), right-click on the project and select "Build". This generates a JAR file that will be utilized later. The location of this jar file is shown in the Output console. In our case, it is

      C:\\workarea\\samples\\javafx\\MetroClientLibrary\\dist\\MetroClientLibrary.jar.
  4. Create JavaFX project
    1. Create a new JavaFX project by right-clicking in the Project explorer, selecting "New Project" and entering the values as shown below:

    2. Click on "Next >" and enter the values as shown below:



      and click on "Finish".
    3. Right-click on the newly created project, "Properties", "Libraries", "Add JAR/Folder" and select the JAR file created in "MetroClientLibrary" project as shown below:



      and click on "OK".

      Notice, Java SE 6 U4 is used to compile and run this project. If you are using an earlier version of Java SE 6, then you need to override JAX-WS 2.1 and JAXB 2.1 jars using endorsed mechanism as explained here. The classes in these jars are already bundled in Java SE 6 U4.
    4. In metroclient.Main.fx file, replace "// place your code here" with the following code:

      import java.lang.\*;
      import javafx.ui.\*;

      import client.NewWebServiceService;
      import client.NewWebService;

      class InputModel {
          attribute name: String?;
      }
      var inputModel = InputModel { };
      var nameField = TextField { };
      nameField.action = operation() {
          inputModel.name = nameField.value;
      };

      class ButtonClickModel {
          attribute result: String;
      }
      var model = new ButtonClickModel();

      Frame {
          title: "JavaFX Client -> Metro endpoint"
          width: 350
          height: 200
          content: GridPanel {
              rows: 3
              vgap: 5
              cells:
              [SimpleLabel {
                  text: "Name : "
              },
              nameField,
              SimpleLabel {
                  text: "Result from endpoint : "
              },
              Label {
                  text: bind "{model.result}"
              },
              Button {
                  text: "Invoke Web Service!"
                  action: operation() {
                      do {
                          try {
                              var service: NewWebServiceService = new NewWebServiceService();
                              var port: NewWebService = service.getNewWebServicePort();
                              var name: String = "{nameField.value}";
                              var result: String = port.sayHello(name);
                              System.out.println("response: {result}");
                              model.result = result;
                          } catch (e:Exception) {
                              System.out.println("exception: {e}");
                          }
                      }
                  }
              }
              ]
          }
          visible: true
      };
  5. Invoke the JavaFX client project
    1. Right-click on the recently create project ("MetroClient") and select "Run Project". The following window is displayed:

    2. Enter "Duke" in the text box and click on "Invoke Web Service!" button to see the result as shown below:

After following these steps, you have created a JavaFX client that can invoke a Metro endpoint project deployed on GlassFish - all using NetBeans IDE.

Now Metro provides secure, reliable, transactional and .NET 3.0 interoperable Web service. Have you tried/used any of those features in Metro ?

Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.

Technorati: totdd javafx metro glassfish netbeans webservices

Comments:

Ugh! That's looking ugly!!! I think my first Swing application looked way better than this!

I thought JavaFX should be comparable to Flash?! Why do you do such ugly examples then? Isn't it easy to do nice looking examples with it? If not, what is it for??

Posted by Christian on January 31, 2008 at 01:50 AM PST #

You certainly can do nicer and fancier UIs with JavaFX but this blog highlights how the configuration would look like. Please feel free to provide an updated UI :)

Posted by Arun Gupta on January 31, 2008 at 08:48 AM PST #

Why You don't do screencasts anymore? I loved them.

Posted by Lucek on February 01, 2008 at 06:14 PM PST #

Arun, i love you . Keep doing the good job

Posted by emeka on February 04, 2008 at 10:34 PM PST #

Hi Sir,

This is uday shankar.I had met with you at sun tech days conference at hyderabad and i felt very happy to attend very extraordinary minds like you.Im very much interested in learning Java Fx, can u help me out in doing so.

Thanks & Regards
Uday Chow

Posted by uday shankar on March 22, 2008 at 07:46 PM PDT #

Uday, you can find all JavaFX related material at: http://openjfx.org.

-Arun

Posted by Arun Gupta on March 23, 2008 at 04:23 PM PDT #

Hi Arun:

I followed the steps you outlined but I get following error. I have posted this on news group but no answer. Can you help?

java.lang.NoClassDefFoundError: [Ljavax/xml/ws/WebServiceFeature;
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getDeclaredMethods(Class.java:1791)

Posted by Tushar on April 05, 2008 at 11:16 PM PDT #

Tushar,

What version of JDK are you using ? JDK 6u4 has JAX-WS classes built-in otherwise you'll have to explicitly include the classes in your classpath.

-Arun

Posted by Arun Gupta on April 15, 2008 at 03:12 AM PDT #

@Chrisitian

If you want slick, try looking at some of the demos over at OpenJFX:

https://openjfx.dev.java.net/downloads.html#demos

Posted by guest on April 16, 2008 at 11:26 PM PDT #

Thanks that was a nice example. We have recently implemented all our web services using Metro on tomcat and Glassfish, so this looks like it might make a good graphical front end sometime in the future. Looking fowards to seeing more example along the way.

Posted by Andrew on May 08, 2008 at 07:08 PM PDT #

christian you said in your comment "Ugh! That's looking ugly!!! I think my first Swing application looked way better than this!

I thought JavaFX should be comparable to Flash?! Why do you do such ugly examples then? Isn't it easy to do nice looking examples with it? If not, what is it for??" why dont you put your wing application here! I tihnk It is not good for you. Because you dont have enough courage for it, so stop doing this.

Posted by Disney Pictures on March 08, 2009 at 07:46 AM PDT #

thanks for your article... very nice.

But I had try that JavaFX to call Web Service, is ok.
But when I run as applet, cannot call web service...
It's seems security issue...

I had see one article at IBM website, saying that we can use applet to call web at the same host, and then use web to call web service...
this make applet can call web service.

But, It's dirty. Is there any solution that JavaFX applet can call remote web service directly ?

Posted by Rocky Wang on April 07, 2009 at 02:03 PM PDT #

Rocky, please ask your question on JavaFX forums at: http://forums.sun.com/category.jspa?categoryID=132 for a better response on it.

Posted by Arun Gupta on April 17, 2009 at 09:06 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

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