Web Services Programming with JBoss

Platform: Eclipse Juno, JBoss AS 7.1.1, Visual Studio 2010

Web Services at a Glance

The distributed application programming has been being around us for many years, even long time before this article was written. The typical method used to make the applications communicate each other is using the Remote Procedure Calls (RPC). Around ten to fifteen years ago (at least) the two most common RPC widely used by the programmers are Distributed Component Object Model (DCOM) and Common Object Request Broker Architecture (CORBA). DCOM is very much familiar among the Visual Studio programmers as it runs in the Microsoft platform, while CORBA is more popular in the Unix environment although it is OS independence (Linux/Unix, Windows).

The classic RPC method like DCOM and CORBA was very dominant in their time. In the past time thousands of computer applications had been written to support collaborative works used by simultaneous users over the Local Area Network (LAN). Communication and inter connections among the applications to exchange some data or to support the client/server model was very common until today. DCOM and CORBA worked perfectly well in that situation because all the communication was made inside the network. Then it started to be a challenge when the time came where there was a need for an organization to exchange data among the other organizations: a local network tried to talk to other local networks. Programmers began to build middleware to establish connections between one point and another point; and were exploring the best ways to exchange information and to provide/consume services to/from the other point. The computer network was expanding.

The Internet web was a strange thing and expensive until finally in 1990s some applications which remotely accessible by multi users were written by programmers, like the Bulletin Board System – which initiated the early social network like the Facebook today. BBS was a very classic application which already extinct today, but we could learn one thing that Internet web was reliable for a communication channel. If Internet could be used for fun stuffs, academy, lab research then it might work also for business. There are many interesting histories about the birth of Internet. Please search “ARPANET” in Google.

Then there was a big bang in around the year 2000. The World Wide Web began to evolve rapidly and nowadays Internet is becoming one giant network that unifies all computer systems around the world without distance limitations. There are certain communication protocols used by various systems to establish the communication links over the Internet web but there is one favorite protocol among the others: the HTTP – the light communication protocol that allow the messages to travel quickly across the Internet web with small overhead. The Internet evolution and the increasing needs to exchange messages across systems drove the birth of Web Services.

The intention is not to replace the existing RPC method like DCOM and CORBA, instead to improve the bridging capability and leverage the existing technology. Web Services allows the RPC to be mode more open across platforms without tied to a specific technology, operating system, programming language and so forth. Today Web Services is very commonly used in many implementations to enable Internet communication between applications not only within the local network but through the Internet backbone as well. What can be more exiting than this?

While HTTP is the communication protocol for Web Services, Simple Object Access Protocol (SOAP) is the data protocol that forms the structure of the message (how the message is formatted) before it is exchanged among systems. A SOAP format is based on XML. As simple as that.

In this article we are going to learn how to develop a web service, host it in an application server and call it through a client programs. We will develop a web service using Java language and use a .NET client program just to show the cross platform interoperability really works.

Creating the Web Service

The first step is to create the web service which will serve the RPC. The program is an Enterprise Java Bean (EJB) that will be hosted in the JBoss Application Server. The demo program is a simple calculator service that accepts remote calls to do some calculations such as addition and subtraction. It is really a simple one, but will give you a very good basic idea of the Web Services programming.

  1. Open Eclipse and create a new Enterprise Application Project (EAR project).

jboss-ws01

Name the project as “CalculatorWS Demo” and ensure the selected target runtime is JBoss AS. Click Finish to create the project files.

  1. Create a new EJB Project.

Name the project as CalculatorBean and ensure the selected target runtime is JBoss AS. Also ensure to add the project to the “CalculatorWS Demo” EAR.

jboss-ws02

Click Next to continue.

  1. Remove the check mark from the “Create an EJB Client JAR…” option.

jboss-ws03

Click Finish to create the EJB project.

If you create the projects correctly, then you will see these two projects in the Project Explorer pane as follow.

jboss-ws04

Now let’s create a calculator Class which will perform the calculation.

  1. Right click on the ejbModule directory under the CalculatorBean project and choose New à Package. Enter demo.calculator.ws as the package name.
  2. Right click on the new package name and choose New –> Interface.

jboss-ws05

Name the Interface as Calculator.

Type the following code for the Interface declaration.

The Interface is quite self explanatory. It tells us the methods that it has: AddNumbers(), SubtractNumbers(), MultiplyNumbers() and DivideNumbers().

  1. Now we want to implement the Interface by writing the action for each method. Right click on the package name and choose New –> Class.

jboss-ws06

Name the new Class as CalculatorImpl. The name implies that it is the implementation of an Interface. Type the following code to implement those four methods defined in the Interface.

 The following line in the Class defines the name of the service as CalculatorService which points to the demo.calculator.ws.Calculator Class.

 

  1. As you look at the Project Explorer pane, you will discover under the CalculatorBean project there is a web service called CalculatorService. It is exactly the same with the service name defined by the line above. The four methods inside the service also correspond to the methods defined within the Class.

jboss-ws07

 

We are done with the web service. Now it is time to package the project and deploy into the application server.

  1. Locate the “CalculatorWS Demo” project (the EAR project) in the Project Explorer pane, and right click on it. Select Export –> EAR file.

jboss-ws08

  1. Enter the Destination file name as CalculatorWSDemo.ear. Click Finish.
  2. Start the JBoss AS and login through the administration console as an administrator.

jboss-ws09

  1. Go to the Runtime tab and select the “Manage Deployments” menu. Click the “Add Content” button. Locate and select the CalculatorWSDemo.ear file generated earlier.
  2. After the EAR file is loaded into the application server, it is not active yet by default. The deployment should be enabled manually to make it active. Press the Enable button for the corresponding deployment.

jboss-ws10

That’s it. We have done deploying the web service. Now it is time to verify the installation.

First, verify in the JBoss AS that the web service appears in the list. Click the Webservices menu as shown in the picture below, and then make sure the web service name is there. If it is not as expected, please review the steps from the beginning. You should have missed something.

jboss-ws11

Second, open the WSDL URL shown in the picture above in a web browser. In this example:

http://localhost:8080/CalculatorBean/CalculatorService/CalculatorImpl?wsdl

The WSDL shown in the web browser should be something like this.

jboss-ws12

WSDL stands for Web Service Definition Language (WSDL), it describes the functionality offered by a web service.

If you succeed the two verifications above, then you are assured the web service is properly deployed in the application server.

Creating the Web Service Consumer

The next step is to create a client program that will consume the web service. First we will create a Java client program and afterward we will create another one in .NET.

  1. Open Eclipse and create a new Java Project named as “CalculatorBean ClientApp”.
  2. In the Libraries tab, ensure to add an External JAR called jboss-client.jar which resides in the ..\bin\client directory under the JBoss installation directory.

jboss-ws13

Click Finish to generate the project.

  1. Create a Package named demo.calculator.ws.
  2. Create an Interface called Calculator under this package with a declaration exactly the same with the Calculator Interface in the CalculatorBean project earlier. Alternatively you can just copy and paste the Interface into this project.
  3. Create the second Package named demo.calculator.client.
  4. Create a Class named MainClass under this package and write the following code.

 

The program tries to open the WSDL URL to locate the CalculatorService web service. After the web service connection is established, the associated methods in the web service are called one by one through the Calculator Interface. The program output is shown as follow.

jboss-ws14

 

Creating the .NET Client

To prove that a web service is an open system accessible from different platform, we will create another web service client other than Java. In this example we are going to create a simple .NET program written in C# which calls the same web service called by a Java program earlier.

  1. Open MS Visual Studio application. The version used this example is Visual Studio 2010, but you can use another version already installed on your PC. Don’t bother.
  2. Create a new “Windows Form Application” project. You are free to give any name.
  3. Design the existing Form as follow, by putting some controls like the label, text field and buttons.

jboss-ws15

  1. Add a new Service Reference and then enter the Address that points to the WSDL URL earlier. Press the Go button to verify the existence and to get the web service declaration.

jboss-ws16

Press OK.

  1. Write the following script in the Form.

 

Save and run the Visual Studio project. The example of the program output is shown in the picture below.

jboss-ws17

The web service works perfectly fine in Visual Studio as in Java. If you wish you may try using another language that supports Web Services.

 

References:

 

tobuku-logo

Leave a Reply

Your email address will not be published. Required fields are marked *