Download presentation
Presentation is loading. Please wait.
Published byNatalie Wheeler Modified over 9 years ago
1
CS 157B: Database Management Systems II February 27 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak
2
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 2 Oral Presentations Back by popular demand! Next Monday, March 4. 4 teams from each section that didn’t present last time. First 4 teams to send me email, or I will “volunteer” teams. Short 15-minute presentations about your Project #2. What is your application? What XML data did you use? How did you do marshalling and unmarshalling? What XQuery queries did you do? PowerPoint slides OK. Presenting teams can turn in your presentation slides in place of a project report. Presenting teams have until Monday to turn in your projects.
3
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 3 Review: Web Services A web service is “a software system designed to support interoperable machine-to-machine interaction over a network”. The service is provided by a server machine (the web service provider). Client applications make service requests to the server and get back responses. A popular form of distributed computing. There are many public web services available. Examples: http://www.service-repository.com/ http://www.service-repository.com/ http://www.webservicex.net/WS/WSDetails.aspx?CATID=2&WSID=9 http://www.webservicex.net/WS/WSDetails.aspx?CATID=2&WSID=9 You can write a web service client program in Java to connect to a web service and consume its data.
4
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 4 Review: Web Services A web service is described by a service contract written in the Web Services Description Language (WSDL). The WSDL document is an XML document. The WSDL document and the request and response messages are transmitted over HTTP. Messages use the Service Oriented Architecture Protocol (SOAP), an XML format. Web services are programming language agnostic. The web service provider code can be written in any language. The web service requester (client) code can be written in any language. XML is the intermediary WSDL document SOAP messages Web services are a major application of the usefulness of XML.
5
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 5 Web Service Example: Time Server A simple web service provider written in Java. Provide the current time as a string. Provide the elapsed Unix time in milliseconds as a long. The required JAXB and JAX-WS packages are part of standard Java 6 and beyond. _
6
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 6 Time Server Provider Time service interface: package wsdemo.timeservice; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; import javax.jws.WebMethod; import javax.jws.WebService; @WebService @SOAPBinding(style = Style.RPC) public interface TimeServer { @WebMethod String getTimeAsString(); @WebMethod long getTimeAsElapsed(); } Service Endpoint Interface (SEI). How to construct the WSDL document Service operations Adopted from Java Web Services: Up and Running by Martin Kalin O’Reilly, 2009
7
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 7 Time Server Provider Time service implementation: package wsdemo.timeservice; import java.util.Date; import javax.jws.WebService; @WebService(endpointInterface = "wsdemo.timeservice.TimeServer") public class TimeServerImpl implements TimeServer { public String getTimeAsString() { return String.format("The current server time is %s", new Date().toString()); } public long getTimeAsElapsed() { return new Date().getTime(); } Link this Service Implementation Bean (SIB) to the TimeServer SEI. Current time operation Elapsed time operation Adopted from Java Web Services: Up and Running by Martin Kalin O’Reilly, 2009
8
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 8 Time Server Publisher Publish the time service: package wsdemo.timeservice; import javax.xml.ws.Endpoint; public class TimeServerPublisher { public static void main(String args[]) { TimeServer ts = new TimeServerImpl(); String url = String.format("http://localhost:%s/ts", args[0]); System.out.printf("Publishing service %s to %s...\n", ts.getClass().getName(), url); // 1st argument is the publication URL // 2nd argument is an SIB instance Endpoint.publish(url, ts); } Pass in the server port number as a runtime argument. Run forever until terminated. Construct the service. Adopted from Java Web Services: Up and Running by Martin Kalin O’Reilly, 2009 ts is an arbitrary name. A web service is usually hosted by a web server running server software such as Apache Tomcat.
9
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 9 Time Service WSDL Download the WSDL document (the service contract) at http://localhost:9876/ts?wsdlhttp://localhost:9876/ts?wsdl...... The portType is similar to a Java interface: It groups the operations provided by the web service. The service endpoint.
10
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 10 Time Service Client The client program makes the time service requests: package wsdemo.timeservice; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; class TimeClient { public static void main(String args[]) throws MalformedURLException { URL url = new URL(String.format("http://localhost:%s/ts?wsdl", args[0])); QName qname = new QName("http://timeservice.wsdemo/", "TimeServerImplService"); Service service = Service.create(url, qname); TimeServer eif = service.getPort(TimeServer.class); System.out.println(eif.getTimeAsString()); System.out.println(eif.getTimeAsElapsed()); } Pass in the server port number as a runtime argument. Create the qualified service name. Factory to get the service port (endpoint interface). Request services. Get the WSDL. Adopted from Java Web Services: Up and Running by Martin Kalin O’Reilly, 2009
11
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 11 Connecting to an Existing Web Service More often, we want to connect to an existing web service in order to consume the data it provides. Connect to the web service via its URL. Download its WSDL service contract. Write the client application to consume and process the data. The client application can be written in any language, such as Java. You don’t know (nor should you care) what language the web service provider is written in. If you write the client application in Java, there are tools that will help. _
12
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 12 The wsimport Utility Program The Java utility program wsimport helps you to write a Java client program to connect to a web service. wsimport reads the WSDL service contract. wsimport generates Java source code that your client application can use. It can also compile the generated source code and create a jar file. Command line: -keep to keep the generated Java source files -s source-directory where to put the generated source files -d class-directory where to put the compiled files -p package for the generated source files wsdl-url to access the WSDL service contract Also: -verbose and –clientjar jarfile wsimport -keep -s java-directory -d class-directory -p package wsdl-url
13
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 13 NetBeans Support for Web Services Tutorial: http://netbeans.org/kb/docs/websvc/jax-ws.htmlhttp://netbeans.org/kb/docs/websvc/jax-ws.html NetBeans supports writing web service provider code and web service client code. For example, if you’re developing client code to access an existing web service, NetBeans calls wsimport for you. _ Demo
14
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 14 Example Public Web Services Try googling: free web services open web services public web services Currency conversion rate http://www.webservicex.net/CurrencyConvertor.asmx http://www.webservicex.net/CurrencyConvertor.asmx Spell checker http://wsf.cdyne.com/SpellChecker/check.asmx http://wsf.cdyne.com/SpellChecker/check.asmx Stock quotes http://www.webservicex.net/stockquote.asmx http://www.webservicex.net/stockquote.asmx Weather reports http://wsf.cdyne.com/WeatherWS/Weather.asmx http://wsf.cdyne.com/WeatherWS/Weather.asmx Demo
15
Department of Computer Science Spring 2013: February 27 CS 157B: Database Management Systems II © R. Mak 15 Project #3 A web services mashup! Create a new web service by combining data from two or more existing web services. Your new web service provider will itself be a client to each of the existing web services. Deploy your web service. Use the sample “publisher” code. Create a Java client to your mashup web service. It should have some meaningful output. Watch the class website for the official write-up. _
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.