• 沒有找到結果。

The Detail Implementation for GT4 Java WS Core

Chapter 4 Comparison of Programming Styles

4.5 The Detail Implementation for GT4 Java WS Core

From Chapter 2, we know that the existence of Web Services Notifications in GT4 Java WS core is to notify changes of resource property to clients who need that info.

As shown in Figure 4-2, for example, there is a grid service, SystemHealthService, returning to users if the system is healthy. In this example, the SystemHealthService also needs other grid services including CPUService returning if the CPU usage is greater than 95% and StorageService returning if the storage usage is greater than 90%. Here “CPU usage” and “Storage usage” are regarded as resource properties.

When the resource properties change, the change info will be sent to SystemHealthService and SystemHealthService will use that info to construct its service’s logistic.

Figure 4-18 WSN in GT4 Java WS Core Grid Service

Grid Service Grid Service

System Health

CPU usage > 95%

Storage usage > 90%

Grid Environment

Now let us take a look at how users utilize GT4 Java WS core to send and receive messages. Figure 4.18 demonstrates the scenario for users sending and receiving messages with GT4 Java WS core. It deserves to be mentioned that the resource property is the so-called Topic in JMS and sending messages to Topic means to change the value of the resource property.

Figure 4-19 The Detail Implementation for GT4 Java WS Core Sender

The following describes the process step by step:

1. First, when the service container starts, the MessageSenderService developed by a user will declare a resource property which is regarded as a Topic and used for message communication.

Figure 4-20 The Detail Implementation for GT4 Java WS Core – Step 1

/* Resource Property set */

private ResourcePropertySet propSet;

/* Resource properties */

private ResourceProperty mesgRP;

/* Topic list */

private TopicList topicList;

/* Create RP set */

this.propSet = new SimpleResourcePropertySet(

PubSubQNames.RESOURCE_PROPERTIES);

/* Initialize the RP's */

try {

mesgRP = new SimpleResourceProperty(PubSubQNames.RP_MESG);

mesgRP.add("no mesg");

} catch (Exception e) {

throw new RuntimeException(e.getMessage());

}

/* Configure the Topics */

this.topicList = new SimpleTopicList(this);

mesgRP = new ResourcePropertyTopic(mesgRP);

((ResourcePropertyTopic) mesgRP).setSendOldValue(true);

this.topicList.addTopic((Topic) mesgRP);

this.propSet.add(mesgRP);

2. The receiver is going to act as a notification consumer. This means that the receiver will have to expose a callback function that will be invoked by the notification producer. For this to happen, the receiver has to act as both a client and a server. Fortunately, thanks to a Globus-supplied class called NotificationConsumerManager, it will help us to do so. In this step, the receiver creates a NotificationConsumerManager.

Figure 4-21 The Detail Implementation for GT4 Java WS Core – Step 2

3. The receiver uses the NotificationConsumerManager created in step 2 to create a NotificationConsumer and assigns itself implementing NotifyCallback interface as a parameter to the NotificationConsumer which will act as a client. And then the receiver will get the EPR of NotificationConsumer since the EPR will be used by the NotificationProducer to deliver the messages.

Figure 4-22 The Detail Implementation for GT4 Java WS Core – Step 3

NotificationConsumerManager consumer;

consumer = NotificationConsumerManager.getInstance();

consumer.startListening();

EndpointReferenceType consumerEPR = consumer

.createNotificationConsumer(this);

4. The receiver uses the EPR of NotificationProducer to locate the NotificationProducerService.

Figure 4-23 The Detail Implementation for GT4 Java WS Core – Step 4

5. The receiver passes the EPR of NotificationConsumer gotten in step 3 as a parameter to the located NotificationProducer.

Figure 4-24 The Detail Implementation for GT4 Java WS Core – Step 5

// Get a reference to the NotificationProducer portType WSBaseNotificationServiceAddressingLocator notifLocator = new WSBaseNotificationServiceAddressingLocator();

EndpointReferenceType endpoint = new EndpointReferenceType();

endpoint.setAddress(new Address(serviceURI));

NotificationProducer producerPort =

notifLocator.getNotificationProducerPort(endpoint);

// Create the request to the remote Subscribe() call Subscribe request = new Subscribe();

// Must the notification be delivered using the Notify operation request.setUseNotify(Boolean.TRUE);

// Indicate what the client's EPR is

request.setConsumerReference(consumerEPR);

// The TopicExpression specifies what topic we want to subscribe to

TopicExpressionType topicExpression = new TopicExpressionType();

topicExpression.setDialect(WSNConstants.SIMPLE_TOPIC_DIALECT);

topicExpression.setValue(PubSubQNames.RP_MESG);

request.setTopicExpression(topicExpression);

6. In this step the receiver actually sends the subscription request to GT4 Java WS core by invoking the method subscribe of NotificationProducer. GT4 Java WS core will then monitor if the resource properties change or not.

Figure 4-25 The Detail Implementation for GT4 Java WS Core – Step 6

7. The sender uses the EPR of MessageSenderService to locate the MessageSenderService.

Figure 4-26 The Detail Implementation for GT4 Java WS Core – Step 7

// Start the ball rolling...

producerPort.subscribe(request);

PubSubServiceAddressingLocator instanceLocator = new PubSubServiceAddressingLocator();

try {

EndpointReferenceType instanceEPR;

if (args[0].startsWith("http")) { String serviceURI = args[0];

// Create endpoint reference to service

instanceEPR = new EndpointReferenceType();

instanceEPR.setAddress(new Address(serviceURI));

} else {

// First argument contains an EPR file name String eprFile = args[0];

// Get endpoint reference of WS-Resource from file FileInputStream fis = new FileInputStream(eprFile);

instanceEPR = (EndpointReferenceType)

PubSubPortType pubsub = instanceLocator .getPubSubPortTypePort(instanceEPR);

8. The sender executes the operation provided by MessageSenderService to change the value of the resource property declared initially. Actually changing the value of resource property means sending messages to the specific topic.

Figure 4-27 The Detail Implementation for GT4 Java WS Core – Step 8

9. If GT4 Java WS core detects that the resource property is changed, the receiver will be notified and received the changed value which is the message sent by the sender.

Figure 4-28 The Detail Implementation for GT4 Java WS Core – Step 9

// Perform operation

for (int i = 0; i < Total_Message; i++) pubsub.publish(sb.toString());

/* This method is called when a notification is delivered */

public void deliver(List topicPath, EndpointReferenceType producer, Object message) {

if (Num_Message == 1) stime = System.currentTimeMillis();

else if(Num_Message == Total_Message) {

etime = System.currentTimeMillis();

// output

System.out.println("Throughput:\t" + (Data_Size * (Total_Message-1) *1000.0)/(etime-stime));

相關文件