Академический Документы
Профессиональный Документы
Культура Документы
Apache Tuscany
Developing an SCA Java RMI service
27 Nov 2007
The Eclipse SOA Tools Platform (STP) plug-in and Apache Tuscany simplifies
services development through the use of the popular Eclipse development
environment. Apache Tuscany has also been integrated with the STP to provide a
Service Component Architecture (SCA) Java™ run time for the services you create,
allowing you to annotate your service using the SCA standard and Apache Tuscany
annotations. In this tutorial, you will see STP and Apache Tuscany in action, through
the creation of a Remote Method Invocation (RMI) service.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 1 of 34
developerWorks® ibm.com/developerWorks
developers with an SCA Java run time by allowing Eclipse developers (Eclipse STP
developers, in this case) to use SCA in the development of Java service
components in an SOA environment.
In this tutorial, you're going to build an RMI-based service that will be registered in
the server's RMI naming registry. You'll build the service according to the SCA
specification, implemented via Apache Tuscany, that will become part of your overall
SOA solution. You will learn:
System requirements
This tutorial uses Eclipse and Apache technologies for development with the STP
plug-in:
Eclipse Classic
Eclipse is the master platform upon which the Eclipse SOA Tools plug-in runs
on. V3.3.1 was used in this tutorial.
Later, you'll learn exactly how to install and prepare your Eclipse installation to use
for SCA development.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 2 of 34
ibm.com/developerWorks developerWorks®
Section 2. Introduction
SOA development is simplified with the addition of the Eclipse STP plug-ins. This
section shows how this tutorial uses the SCA plug-ins portion of the Eclipse SOA
Tools Platform.
In this tutorial, in addition to the common and core STP plug-ins, we will use the
Service Creation Service Oriented Architecture (SC.SCA) plug-ins. They, through
the integration of Apache Tuscany, are what allows you to annotate your service
using SCA and Apache Tuscany service annotations.
Apache Tuscany effectively provides you with a SCA Java run time by allowing you
to use SCA in the development of Java components in an SOA environment. Think
of Apache Tuscany as the powerhouse that implements the SCA specification,
making SCA a reality, with the Eclipse STP plug-ins tying everything together —
from properties and code to run-time configurations and debugging — allowing the
construction of a true SOA solution.
You should now have a good idea of how SOA and SCA work together, so let's go
over some details of the example by previewing the Time Bomb service that you'll
build in this tutorial.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 3 of 34
developerWorks® ibm.com/developerWorks
specification, we will create the Time Bomb game, which is created more simply with
the help of Eclipse STP. It is an on-your-feet trivia game that will require your
knowledge and fast typing abilities to answer questions within a 60-second time limit.
The service you're going to create and build is a Time Bomb service. There are
tokens (identifiers) in the service that, when submitted to the service, start the Time
Bomb countdown for that token, returning a trivia question that will then need to be
answered within 60 seconds. If you answer incorrectly, the service responds with
how many seconds you have left and continues its countdown. If you are correct, the
service will indicate a "Correct!" response. If you lose and go past 60 seconds, the
service will answer your question with a "KABOOM!" response. If you answer
correctly, the timer resets, after which you'll need to resubmit your token to move
onto the next question.
Install the first, main, Eclipse ZIP file you should have downloaded
(eclipse-SDK-3.3.1-win32.zip for this tutorial) to your programs directory
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 4 of 34
ibm.com/developerWorks developerWorks®
(F:\programs for the purposes of this tutorial) by first moving the ZIP there, followed
by unzipping it. You should now have F:\programs\eclipse.
Last, unzip the Apache Tuscany download, also in your programs directory. This
should create a new directory there: tuscany-sca-1.0-incubating (at
F:\programs\tuscany-sca-1.0-incubating for the purposes of this tutorial). Make note
of this directory because you'll need to point to this directory from within Eclipse.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 5 of 34
developerWorks® ibm.com/developerWorks
The run time should then be listed in the list of Installed Server Runtime
Environments, as shown below.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 6 of 34
ibm.com/developerWorks developerWorks®
That's all the special configuration you need to make. Next, you'll create your first
SOA Tools SCA project.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 7 of 34
developerWorks® ibm.com/developerWorks
Now type the project name, TimeBomb, as shown in Figure 4, and click Next.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 8 of 34
ibm.com/developerWorks developerWorks®
Next, select the Apache Tuscany run time you just added, as shown in Figure 5, and
click Next.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 9 of 34
developerWorks® ibm.com/developerWorks
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 10 of 34
ibm.com/developerWorks developerWorks®
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 11 of 34
developerWorks® ibm.com/developerWorks
Here, you can select a different host, port, or service name for your project. Or you
can leave the defaults as-is and click Finish to create the project. Click Yes to enter
the SCA Java perspective, and you should see your project and its contents as
shown by the Package navigator in Figure 8.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 12 of 34
ibm.com/developerWorks developerWorks®
In the rest of the tutorial, you'll experience the SCA construction provided by the
Eclipse STP project.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 13 of 34
developerWorks® ibm.com/developerWorks
possibly students, registered or otherwise, that will each receive a token, giving them
access to the Time Bomb. Associated with each token is the following:
• Question activateMe(Token)
• The above activateMe function takes in a String token as a
parameter, checks if the token is valid, activates that token, and
assigns a question to it.
• The question assigned to the token is then returned to the sender as
a String.
• If the token sent in is invalid, the following is returned in the response:
"Token invalid!"
• Result submitAnswer(Token, Answer)
• The submitAnswer function takes in the user's Token as the
parameter and checks to see if the user answered the question within
the appropriate time limit.
• If the user failed to respond in the time allotted, the following
response is given: "KABOOM!!! You have spontaneously
combusted! Better luck next time!"
• If the user responded within the appropriate time, the service
tries to match the Answer parameter with the answer to the
question currently assigned to that user.
• If the incoming Answer matches the answer associated
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 14 of 34
ibm.com/developerWorks developerWorks®
package com.ibm.dw.tb;
import org.osoa.sca.annotations.Remotable;
import org.osoa.sca.annotations.Service;
@Service()
@Remotable()
public interface TimeBomb {
public Response activateMe(Request r);
public Response submitAnswer(Request r);
}
Here you have both the activateMe and submitAnswer methods with Request
as a parameter and Response as the response. You can also see the SCA
annotations, the @Service() and @Remotable() annotations above the interface
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 15 of 34
developerWorks® ibm.com/developerWorks
declaration. They indicate to the server that this class is a service and is remotable,
or accessible over the Internet. These two classes, Response and Request, are
what you're going to define next.
package com.ibm.dw.tb;
public class Response implements Serializable {
String response;
public Response(String r){
response = r;
}
public String getResponse(){
return response;
}
public void setResponse(String r){
response = r;
}
}
The class contains a response variable you are able to get and set using getter and
setter methods, respectively.
package com.ibm.dw.tb;
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 16 of 34
ibm.com/developerWorks developerWorks®
Here, you see the token and answer String variables, which are gettable and
settable via getter and setter methods, respectively.
Next, you'll begin coding the TimeBombImpl class, the class containing the
business methods of the service.
package com.ibm.dw.tb;
import org.osoa.sca.annotations.Service;
import java.util.Random;
@Scope("REQUEST")
@Service(com.ibm.dw.tb.TimeBomb.class)
public class TimeBombImpl implements TimeBomb {
static final String FAILURE =
"KABOOM!!! You have spontaneously combusted! Better luck next time!";
static final String CORRECT =
"Phew!!! You are saved from imminent spontaneous combustion!";
static final String INCORRECT1 = "Uh oh!!! You better get your facts " +
"straight or spontaneous combustion is imminent! You have ";
static final String INCORRECT2 = " seconds left!";
static final String INVALID_TOKEN = "Token invalid!";
static final int secondsToAnswer = 60;
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 17 of 34
developerWorks® ibm.com/developerWorks
...
}
Here, you can see the class definition begin and that it implements the TimeBomb
interface. There are then six variables beginning with static final. These are
constants that contain the messages returned for invalid tokens, failed and correct
responses. The last one sets how many seconds the user has to answer before time
runs out. It also specifies an SCA annotation, which specifies the interface class to
use in programmatically representing the RMI service. It also specifies an SCA
annotation (@Service), which specifies the interface class to use in
programmatically representing the RMI service. The other SCA annotation
(@Scope) specifies that an individual thread will be allocated to serve requests for
each client, allowing the use of local variables.
Storing tokens
The tokens are going to be stored in arrays, along with the associated values,
including whether the token is valid, which token is currently assigned to the token,
and the start time that the question was assigned to the user. Add the following
variable declarations to your TimeBombImpl.java class.
The tokens String array contains valid tokens (user IDs) accepted by the Time
Bomb service. Each token is then associated with:
• A value in the valid Boolean array, which specifies whether the token is
valid
• A value in the assignedQuestions array, which specifies the question
number currently assigned to the user
• A value in the startTimes array, which specifies the start time when the
token was first activated and assigned a question
That's all that's needed for the tokens. Next, you'll see how to store questions and
associated answers.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 18 of 34
ibm.com/developerWorks developerWorks®
The first questions and answers are declared as String arrays. The code in the
static{ statement gets executed when the class is first loaded into memory,
enabling the questions and answers arrays to be initialized appropriately. Note that
both of them are initialized as String arrays of length 5, and each of the five
questions and answers are initialized appropriately.
Validating a token
Each token sent into both the activateMe and submitAnswer functions needs to
be validated against the ones defined in Listing 6. Remember that tokens are
case-sensitive. Add the following function to your TimeBombImpl class.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 19 of 34
developerWorks® ibm.com/developerWorks
}
return -1;
}
Here, you simply loop through all available tokens, and if the one passed into the
service matches one of the valid tokens, return the index, i, that matched the token.
Otherwise, return -1 (an invalid token). You'll use this index to assign a question to
each token and set the appropriate start time.
The function starts off in the first two lines by validating the token. If the token is
valid, validToken will not equal -1. If the token is invalid, the response is simply
set to NVALID_TOKEN, and the method returns a response. Otherwise, a random
number generator is created, and a random integer between 0 (inclusive) and 4
(inclusive) is generated. Thus, five possible values for each of the five questions and
answers defined in Listing 7. After the question number is determined, the question's
text is set as the value in the response, the appropriate position in the valid array
is set to true, the appropriate position in the assignedQuestions array is set to
questionNum, and the appropriate position in the startTime array is set to the
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 20 of 34
ibm.com/developerWorks developerWorks®
return response;
}
You begin by retrieving and validating the token as in Listing 9, while grabbing the
answer out of the request, as well. This time, you also make sure that the token is
valid (not equal to -1) and that the token already has a question assigned to it
(valid[validToken] == true). If the token is valid and has a question
assigned to it, you first need to make sure the answer has come within the given
time limit of 60 seconds by comparing the two times, making sure that less than 60
seconds has elapsed since the question was assigned. If the answer was received
in the allowed time frame, you need to compare the answer given to the answer to
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 21 of 34
developerWorks® ibm.com/developerWorks
the question as defined in Listing 7. If the answers match, you set the response text
to the String, as defined in the constant CORRECT (see Listing 5).
Next, you'll define the response for answers not matching, yet are within the
60-second time limit.
Listing 11. Setting the response for an incorrect answer within the time limit
Here, you are indicating to the user how much time is left to save himself from
spontaneous combustion. Next, you define the response for answers received after
the 60-second time limit has expired.
Receiving a response to a question outside the time limit: KABOOM! Uh-oh. The
time limit has expired, so the message defined in FAILURE (see Listing 5) needs to
be returned in the response. Define the code for this response in Listing 12.
Listing 12. The response for answering after time has expired
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 22 of 34
ibm.com/developerWorks developerWorks®
Here, in the second else statement, you set the response text to the value held in
the FAILURE constant.
Here, the token is simply invalidated by setting the appropriate position in the valid
array to false.
This completes all the code for the service. Next, you'll run the server.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 23 of 34
developerWorks® ibm.com/developerWorks
There you have it. You can close down the server by clicking the red square button
or by clicking Enter.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 24 of 34
ibm.com/developerWorks developerWorks®
• Your New Java Interface window should appear, as shown in Figure 11.
Figure 11. New Java Interface window
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 25 of 34
developerWorks® ibm.com/developerWorks
• Click Finish
Now to create the class:
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 26 of 34
ibm.com/developerWorks developerWorks®
• Click Finish
Last, you need to copy your Request.java and Response.java from the TimeBomb
project to your TimeBombRMIClient project. Select them in the Navigator pane
and drag them to your TimeBombRMIClient package, as shown below.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 27 of 34
developerWorks® ibm.com/developerWorks
Great! The client is ready for you to add your magic, er, code.
package com.ibm.dw.tb;
import java.rmi.Remote;
public interface TimeBomb extends Remote {
public Response activateMe(Request r);
public Response submitAnswer(Request r);
}
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 28 of 34
ibm.com/developerWorks developerWorks®
It's essentially the same except that it extends Remote and doesn't contain any SCA
annotations. Instead, it extends the java.rmi.Remote class, allowing this class to
hold the reference to your remote service, which you'll invoke next in the
TimeBombRMIClient class.
package com.ibm.dw.tb;
import java.rmi.Naming;
public class TimeBombRMIClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
TimeBomb service = (TimeBomb)
Naming.lookup("rmi://localhost:1099/TimeBombRemoteService");
String token = null;
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Enter your token:");
try {
token = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your token!");
}
Response r = service.activateMe(new Request(token, null));
System.out.println("Question: " + r.getResponse());
...
} catch (Exception e){
e.printStackTrace();
}
}
}
Within the class declaration, you look up the TimeBomb service through the server's
RMI registry running your TimeBomb service. Then type in your token. Invoking the
service.activateMe remote method passes the token information you entered
along to the remote service. The response from the service is then stored in r. The
question in the response you'll get is one of the five random questions defined back
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 29 of 34
developerWorks® ibm.com/developerWorks
in Listing 7.
Next, you'll code retrieving the answer from the console and send it in as your
response.
...
Response r = service.activateMe(new Request(token, null));
System.out.println("Question: " + r.getResponse());
String answer = null;
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Enter your answer:");
try {
answer = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your answer!");
}
Response r2 = service.submitAnswer(new Request(token, answer));
System.out.println("Result: " + r2.getResponse());
...
The code in Listing 16 allows you to type a response by having the user type an
answer. The answer and token entered earlier are funneled to the remote service in
the call to service.submitAnswer. By playing with the client, you should be able
to receive all three possible response types by typing a correct response within the
time frame, typing an incorrect response within the time frame, and waiting 60
seconds before submitting a response. You can play with the client by submitting
invalid tokens, as well, in order to get the invalid token response.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 30 of 34
ibm.com/developerWorks developerWorks®
Great! Your RMI client has successfully obtained a reference to your RMI service
that you built according to the SCA specification, which also comprises your SOA.
Section 6. Summary
You've successfully installed and used the Eclipse STP plug-in and Apache Tuscany
SCA Java implementation. Additionally, you created an SOA Tools SCA project
using RMI bindings and an RMI client able to successfully communicate with the
service. One possible improvement you might want to make is to have these tokens
and questions grabbed from a database, so you can add to them as necessary
without having to muck with and rebuild the service. You can also of course create
other services taking advantage of Eclipse STP and Apache Tuscany.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 31 of 34
developerWorks® ibm.com/developerWorks
Downloads
Description Name Size Download
method
Sample code os-eclipse-soatptuscany.sca.code.zip
7KB HTTP
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 32 of 34
ibm.com/developerWorks developerWorks®
Resources
Learn
• Check out the Eclipse SOA Tools Platform (STP) project at Eclipse.org.
• Learn about the Eclipse Service Creation (SC) subproject.
• Learn more at the Eclipse STP wiki.
• To learn more about Service-Oriented Architecture (SOA), check out the
developerWorks SOA and Web services zone.
• Learn more about SCA and its relationship to SOA in the article "Service
Component Architecture."
• See another Eclipse STP tutorial titled "Use the Eclipse SOA Tools Platform
plug-in to build and deploy a Web service.
• Answers to your Eclipse questions are available in "What is Eclipse and how do
I use it?"
• Check out the extensive documentation, tutorials, presentations, and
screencasts that illuminate the Eclipse Test & Performance Tools Platform
(TPTP).
• Check out the "Recommended Eclipse reading list."
• Browse all the Eclipse content on developerWorks.
• New to Eclipse? Read the developerWorks article "Get started with Eclipse
Platform" to learn its origin and architecture, and how to extend Eclipse with
plug-ins.
• Expand your Eclipse skills by checking out IBM developerWorks' Eclipse project
resources.
• To listen to interesting interviews and discussions for software developers,
check out check out developerWorks podcasts.
• For an introduction to the Eclipse platform, see "Getting started with the Eclipse
Platform."
• Stay current with developerWorks' Technical events and webcasts.
• Watch and learn about IBM and open source technologies and product
functions with the no-cost developerWorks On demand demos.
• Check out upcoming conferences, trade shows, webcasts, and other Events
around the world that are of interest to IBM open source developers.
• Visit the developerWorks Open source zone for extensive how-to information,
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 33 of 34
developerWorks® ibm.com/developerWorks
tools, and project updates to help you develop with open source technologies
and use them with IBM's products.
Get products and technologies
• Download the Eclipse STP plug-in and required plug-ins.
• Download Apache Tuscany SCA Java.
• Check out the latest Eclipse technology downloads at IBM alphaWorks.
• Download Eclipse Platform and other projects from the Eclipse Foundation.
• Download IBM product evaluation versions, and get your hands on application
development tools and middleware products from DB2®, Lotus®, Rational®,
Tivoli®, and WebSphere®.
• Innovate your next open source development project with IBM trial software,
available for download or on DVD.
Discuss
• Subscribe to the STP user list.
• Subscribe to the STP developer list.
• The Eclipse Platform newsgroups should be your first stop to discuss questions
regarding Eclipse. (Selecting this will launch your default Usenet news reader
application and open eclipse.platform.)
• The Eclipse newsgroups has many resources for people interested in using and
extending Eclipse.
• Participate in developerWorks blogs and get involved in the developerWorks
community.
Using the Eclipse SOA Tools Platform plug-in and Apache Tuscany Trademarks
© Copyright IBM Corporation 2007. All rights reserved. Page 34 of 34