Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Create notification producer and consumer using tooling
Contents
Create notification producer and consumer using tooling
Author: | Krishna C Shastry, Saurabh Dravid | |
email: | krishna.shastry@in.ibm.com | sadravid@in.ibm.com |
Last updated: |
Create a notification producer resource
- We will take a file system as a notification producer resource, which will notify the consumer at some specific time interval about the file space change event.
-
Create a new Manageability Endpoint Project by the name of ProducerModel. All our Notification producer related artifacts will be stored in this project.
- For this resource we create a capability which publishes the file system space change events to the consumer. Create a new capability by specifying following user inputs defined in following screenshot. Click Finish.
-
Tooling will open this newly created capability with Capability Editor. Move to the Topics tab in the editor and add a new Root Topic by providing the Topic namespace as "http://www.eclipse.org/FileSpaceChangeEvent/capability/Topics" and Root topic name as "FileSpaceChange".
-
Create a new Manageable resource type (WSDM resource type) and add the "NotificationProducer" and "FileSpaceChangeEvent" capability to it.
-
Right click the "FileSystem.mrt" file and select the option Generate Java Code. This will bring a new wizard to do the code generation for defined managed resource type. Provide the Output project name as "FileSystem".
-
After doing the code generation remove the java package "org.eclipse.tptp.wsdm.runtime.capability" from "FileSystem" web project. The classes available in this package are already available in tools.jar.
-
Open Java class in /FileSystem/JavaSource by the name of org.eclipse.www.FileSpaceChangeEvent.capability.MyCapability and put the following code in this class. This class will publish the file system space change notification messages at specific time interval.
package org.eclipse.www.FileSpaceChangeEvent.capability; import javax.xml.namespace.QName; import org.apache.muse.core.AbstractCapability; import org.apache.muse.util.xml.XmlUtils; import org.apache.muse.ws.addressing.soap.SoapFault; import org.apache.muse.ws.notification.NotificationProducer; import org.apache.muse.ws.notification.WsnConstants; import org.w3c.dom.Element; public class MyCapability extends AbstractCapability { public QName TOPIC_QNAME = new QName("http://www.eclipse.org/FileSpaceChangeEvent/capability/Topics","FileSpaceChange","tns"); private final int TIME_INTERVAL_SECONDS = 3; public void initialize() throws SoapFault { // //The following call is necessary to property initialize the resource // super.initialize(); // //TODO Perform any needed initialization for this empty capability // } public void initializeCompleted() throws SoapFault { super.initializeCompleted(); Thread thread = new Thread(){ public void run() { String message = "File space changed"; QName messageName = new QName("http://org.eclipse/FileSystem", "FileSpaceChangeMessage", "tns"); Element payload = XmlUtils.createElement(messageName, message); NotificationProducer producer = (NotificationProducer) getResource() .getCapability(WsnConstants.PRODUCER_URI); while (true) { try { Thread.sleep(TIME_INTERVAL_SECONDS * 1000); } catch (InterruptedException e1) { e1.printStackTrace(); } try { producer.publish(TOPIC_QNAME, payload); } catch (SoapFault e) { e.printStackTrace(); } } } }; thread.start(); } }
Create a notification consumer resource
-
We will prepare a consumer resource which will listen to the file system producer resource and print the received messages to console.
-
Create a new Manageability Endpoint Project by the name of ConsumerModel. All our Notification consumer related artifacts will be stored in this project.
- For this resource we create an empty capability listens to the FileSystemProducer resource. Create a new capability by specifying following user inputs defined in following screenshot. Click Finish.
-
Create a new Manageable resource type (WSDM resource type) and add the "NotificationConsumer" and "FileSpaceChangedEventConsumer" capability to it.
-
Right click the "FileSystemConsumerType.mrt" file and select the option Generate Java Code. This will bring a new wizard to do the code generation for defined managed resource type. Provide the Output project name as "Consumer".
-
Open Java class in /Consumer/JavaSource by the name of org.eclipse.www.FileSpaceChangedEventConsumer.capability and put the following code in this class. This class will listen to the producer and prints the received message to console.
package org.eclipse.www.FileSpaceChangedEventConsumer.capability; import org.apache.muse.core.AbstractCapability; import org.apache.muse.ws.addressing.soap.SoapFault; import org.apache.muse.ws.notification.NotificationConsumer; import org.apache.muse.ws.notification.NotificationMessage; import org.apache.muse.ws.notification.NotificationMessageListener; import org.apache.muse.ws.notification.WsnConstants; public class MyCapability extends AbstractCapability implements NotificationMessageListener { public void initialize() throws SoapFault { super.initialize(); } public void initializeCompleted() throws SoapFault { super.initializeCompleted(); NotificationConsumer consumer = (NotificationConsumer) getResource().getCapability(WsnConstants.CONSUMER_URI); consumer.addMessageListener(this); } public boolean accepts(NotificationMessage arg0) { return true; } public void process(NotificationMessage message) throws SoapFault { System.out.println("I recieved following message" + message); } }
Create a consumer client
- Deploy the resources FileSystem and Consumer on tomacat.
- Create package in /Consumer/JavaSource by the name of org.eclipse.test.client
-
Create a class by the name ConsumerTestClient and put the following code in this class.This will create client which subscribes to the producer resource.
package org.eclipse.test.client; import java.net.InetAddress; import java.net.URI; import java.net.UnknownHostException; import org.apache.muse.ws.addressing.EndpointReference; import org.apache.muse.ws.notification.remote.NotificationProducerClient; public class ConsumerTestClient { public static void main(String[] args) { try { String PRODUCER_URI = "http://localhost:8080/FileSystem/services/FileSystem"; String CONSUMER_URI = "http://localhost:8080/Consumer/services/FileSystemConsumerType"; EndpointReference producerEPR = new EndpointReference(URI.create(PRODUCER_URI)); EndpointReference consumerEPR = new EndpointReference(URI.create(CONSUMER_URI)); // // null filter == send all messages to consumer // NotificationProducerClient producer = new NotificationProducerClient(producerEPR); producer.setTrace(true); producer.subscribe(consumerEPR, null, null); } catch (Throwable error) { error.printStackTrace(); } } }
- Run the java class ConsumerTestClient.This will print the message received to the tomcat console.