Week 5
Download
1 / 62

Week 5 - PowerPoint PPT Presentation


  • 339 Views
  • Updated On :

Week 5. Preliminaries to XML Messaging Introductory Java Threads A multi-threaded server XML Messaging A PowerWarning application using SAX A DOM example Building a DOM Tree from Scratch. Thread examples taken from “The Java Programming Language”

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Week 5' - Roberta


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Week 5 l.jpg
Week 5

  • Preliminaries to XML Messaging

  • Introductory Java Threads

  • A multi-threaded server

  • XML Messaging

  • A PowerWarning application using SAX

  • A DOM example

  • Building a DOM Tree from Scratch

Thread examples taken from “The Java Programming Language”

By Arnold and Gosling and from Cay Horstmann’s “Core Java 2

Advanced”. The PowerWarning Application is from “XML and

Java” by Maruyama, Tamura, and Uramoto, Addison-Wesley.

Internet Technologies


Four kinds of thread programming l.jpg
Four kinds of thread programming

1) Unrelated threads

2) Related but unsynchronized threads

3) Mutually-exclusive threads

4) Communicating mutually-exclusive

threads

We will look at the first two (simple) kinds.

Internet Technologies


Slide3 l.jpg

Unrelated threads

class Coffee extends Thread {

Coffee(String name) {

super(name);

}

public void run() {

for(int n = 1; n <= 3; n++) {

System.out.println("I like coffee");

yield();

System.out.println(this.getName());

yield();

}

}

}

Internet Technologies


Slide4 l.jpg

class Tea extends Thread {

Tea(String name) {

super(name);

}

public void run() {

for(int n = 1; n <= 3; n++) {

System.out.println("I like tea");

yield();

System.out.println(this.getName());

yield();

}

}

}

Internet Technologies


Slide5 l.jpg

public class Drinks {

public static void main(String args[]) {

System.out.println("I am main");

Coffee t1 = new Coffee("Wawa Coffee");

Tea t2 = new Tea(“Sleepy Time Tea");

t1.start();

t2.start();

System.out.println("Main is done");

}

}

Internet Technologies


Slide6 l.jpg

Output

I am main

Main is done

I like coffee

I like tea

Wawa Coffee

Sleepy Time Tea

I like coffee

I like tea

Wawa Coffee

Sleepy Time Tea

I like coffee

I like tea

Wawa Coffee

Sleepy Time Tea

Main finishes right away

Threads are sharing time

This program has three threads.

Internet Technologies


Slide7 l.jpg

Unrelated Threads Part II

  • Using sleep() in unrelated threads

  • The call sleep(millis) puts the currently executing thread to

  • sleep for at least the specified number of milliseconds. "At

  • least“ means there is no guarantee the thread will wake up

  • in exactly the specified time. Other thread scheduling can

  • interfere.

Internet Technologies


Slide8 l.jpg

class Coffee extends Thread {

Coffee(String name) {

super(name);

}

public void run() {

for(int n = 1; n <= 3; n++) {

System.out.println("I like coffee");

try {

sleep(1000); // 1 second

}

catch(InterruptedException e) {}

System.out.println(this.getName());

}

}

}

Internet Technologies


Slide9 l.jpg

class Tea extends Thread {

Tea(String name) {

super(name);

}

public void run() {

for(int n = 1; n <= 5; n++) {

System.out.println("I like tea");

System.out.println(getName());

}

}

}

Internet Technologies


Slide10 l.jpg

public class Drinks2 {

public static void main(String args[]) {

System.out.println("I am main");

Coffee t1 = new Coffee("Wawa Coffee");

Tea t2 = new Tea(“Sleepy Time Tea");

t1.start();

t2.start();

System.out.println("Main is done");

}

}

Internet Technologies


Slide11 l.jpg

I am main

Main is done

I like coffee

I like tea

Sleepy Time Tea

I like tea

Sleepy Time Tea

I like tea

Sleepy Time Tea

I like tea

Sleepy Time Tea

I like tea

Sleepy Time Tea

Wawa Coffee

I like coffee

Wawa Coffee

I like coffee

Wawa Coffee

After “I like coffee”, the coffee thread goes to sleep and the tea thread gets to finish and die.

1 second pausing after each “I like coffee”

Internet Technologies


Yield and sleep l.jpg
Yield() and Sleep()

  • Yield() may have no effect on some implementations.

  • The thread scheduler might make no effort toward fairness.

  • The yielding thread may be picked again even though other threads want a turn.

  • It is a good idea to call sleep() instead.

Internet Technologies


Ping pong l.jpg
Ping Pong

  • Adapted from "The Java Programming Language", Arnold

  • and Gosling

  • After a thread is created, you can configure it – set its name,

  • its initial priority (system dependent), and so on.

  • The start() method spawns a new thread of control based on

  • the data in the thread object and then returns.

  • Now, the Java virtual machine invokes the new thread's run

  • method, making the thread active.

  • When a thread's run method returns, the thread has exited.

  • The thread may be manipulated with a number of methods,

  • including the interrupt() method as shown in this example.

Internet Technologies


Slide14 l.jpg

Each PingPong object knows what to say and how long to sleep.

public class PingPong extends Thread {

private String word;

private int delay;

public PingPong(String whatToSay, int delayTime) {

word = whatToSay;

delay = delayTime;

}

Internet Technologies


Slide15 l.jpg

public void run() { sleep.

try {

for(;;) {

System.out.println(word+" ");

sleep(delay);

}

}

catch (InterruptedException e) {

System.out.println("Interrupted!!!!!");

return;

}

}

Internet Technologies


Slide16 l.jpg

public static void main(String args[]) { sleep.

PingPong t1 = new PingPong("\tping",33);

t1.start();

PingPong t2 = new PingPong("Pong",100);

t2.start();

try {

Thread.sleep(5000);

}

catch(InterruptedException e) {

// will not be printed

System.out.println("Good morning");

return;

}

Internet Technologies


Slide17 l.jpg

Thread myThread = Thread.currentThread(); sleep.

for (int t = 1 ; t <= 10; t++)

System.out.println("In Main..." + myThread.getName());

t1.interrupt();

}

}

Internet Technologies


Slide18 l.jpg

C:\McCarthy\threads\PingPong>java PingPong sleep.

ping

Pong

ping

ping

ping

Pong

ping

ping

ping

Pong

ping

ping

ping

:

:

Main is asleep.

For 5 seconds

ping and pong

take turns sleeping

and running

Internet Technologies


Slide19 l.jpg

Pong sleep.

ping

ping

ping

Pong

In Main...main

In Main...main

In Main...main

In Main...main

In Main...main

In Main...main

In Main...main

In Main...main

In Main...main

In Main...main

Interrupted!!!!!

Pong

Pong

Pong

Pong

Pong

:

Main wakes up

Main interrupts

Ping and ping dies.

“Pongs” forever

or until until ctrl-c

Internet Technologies


A thread application a simple web server l.jpg
A Thread Application --A Simple Web Server sleep.

  • Responds by sending the same file on each hit

  • Creates a new thread on each hit

Internet Technologies


Slide21 l.jpg

// A simple multi-threaded web server sleep.

import java.net.*;

import java.io.*;

import java.util.*;

public class OneFile extends Thread {

static String theData = "";

static String contentType;

static int contentLength;

Socket theConnection;

Internet Technologies


Slide22 l.jpg

// construct each OneFile object with an existing socket sleep.

public OneFile(Socket s) {

theConnection = s;

}

// run the following code on each object

public void run() {

try {

// get a PrintStream attached to this socket

PrintStream os = new PrintStream(

theConnection.getOutputStream());

// get a DataInputStream attached to this socket

DataInputStream is = new DataInputStream(

theConnection.getInputStream());

// read a line from the socket

String request = is.readLine();

Internet Technologies


Slide23 l.jpg

// HTTP/1.0 and later send a MIME header sleep.

if(request.indexOf("HTTP/") != -1) {

// we need to read the rest of the MIME header

while(true) {

String thisLine = is.readLine();

if(thisLine.trim().equals("")) break;

}

// respond to the client

os.print("HTTP/1.0 200 OK\r\n");

// send the date

Date now = new Date();

os.print("Date: " + now + "\r\n");

// send our name

os.print("Server: OneFile 1.0\r\n");

Internet Technologies


Slide24 l.jpg

// send the contentLength sleep.

os.print("Content-length: " + contentLength + "\r\n");

// send the content type

os.print("Content-type: " + contentType + "\r\n\r\n");

}

// send the file in the string

os.println(theData);

theConnection.close();

}

catch(IOException e) {

}

}

Internet Technologies


Slide25 l.jpg

// main loads the file and creates the object on every hit sleep.

public static void main(String args[] ) {

int thePort;

ServerSocket ss;

Socket theConnection;

FileInputStream theFile;

// cache the file

try {

// open file and create a DataInputStream

theFile = new FileInputStream(args[0]);

DataInputStream dis = new DataInputStream(theFile);

Internet Technologies


Slide26 l.jpg

// determine the content type of this file sleep.

if(args[0].endsWith(".html") || args[0].endsWith(".htm") ) {

contentType = "text/html";

}

else {

contentType = "text/plain";

}

// read the file into the string theData

try {

String thisLine;

while((thisLine = dis.readLine()) != null) {

theData += thisLine + "\n";

}

}

catch(Exception e) {

System.err.println("Error " + e);

}

}

Internet Technologies


Slide27 l.jpg

catch(Exception e) { sleep.

System.err.println(e);

System.err.println("usage: java onefile filename port");

System.exit(1);

}

// set the port to listen on

try {

thePort = Integer.parseInt(args[1]);

if(thePort < 0 || thePort > 65535) thePort = 80;

}

catch(Exception e) {

thePort = 80;

}

Internet Technologies


Slide28 l.jpg

// create a server socket sleep.

try {

ss = new ServerSocket(thePort);

System.out.println("Accepting connections on port " +

ss.getLocalPort());

System.out.println("Data to be sent:");

System.out.println(theData);

while(true) {

// stop and wait for a connection

Socket socketTemp = ss.accept();

// we have a socket so create a handler

OneFile fs = new OneFile(socketTemp);

// start the handler running

fs.start();

}

}

catch(IOException e) {System.out.println("Socket error"); }

}

}

Start thread

Internet Technologies


Slide29 l.jpg

Example: XML Messaging sleep.

The PowerWarning application allows users to register

their geographical position and their temperature concerns.

Users will receive e-mail when the temperature exceeds the user

specified parameters.

This example is from

“XML and Java” by Maruyama, Tamura, and Uramoto, Addison-Wesley.

Internet Technologies


Slide30 l.jpg

Suppose that we know that the weather information is available

from the web at

http://www.xweather.com/White_Plains_NY_US.html.

[1] <html>

[2] <head>

[3] <title>Weather Report</title>

[4] </head>

[5] <body>

[6] <h2>Weather Report -- White Plains, NY </h2>

[7] <table border=1>

[8] <tr><td>Date/Time</td><td align=center>11 AM EDT Sat Jul 25

1998</td></tr>

[9] <tr><td>Current Tem.</td><td align=center>70&#176;</td></tr>

[10] <tr><td>Today’s High</td><td align=center>82&#176;</td></tr>

[11] <tr><td>Today’s Low</td><td align=center>62&#176;</td><tr>

[12] </table>

[13] </body>

[14] </html>

Internet Technologies


Slide31 l.jpg

  • Strategy 1: available

  • For the current temperature of White Plains, go to line 9,

  • column 46 of the page and continue until reaching the next

  • ampersand.

  • Strategy 2:

  • For the current temperature of the White Plains, go to the

  • first <table> tag, then go to the second <tr> tag within the

  • table, and then go to the second <tg> tag within the row.

Neither of these seems very appealing…

Internet Technologies


Slide32 l.jpg

<?xml version=“1.0”?> available

<!DOCTYPE WeatherReport SYSTEM

“http>//www.xweather.com/WeatherReport.dtd”>

<WeatherReport>

<City>White Plains</City>

<State>NY</State>

<Date>Sat Jul 25 1998</Date>

<Time>11 AM EDT</Time>

<CurrTemp unit=“Farenheit”>70</CurrTemp>

<High unit=“Farenheit”>82</High>

<Low unit=“Farenheit”>62</Low>

</Weather Report>

XML would help

Internet Technologies


Slide33 l.jpg

  • Strategy 3: available

  • For the current temperature of White Plains, N.Y., go

  • to the <CurrTemp> tag.

Internet Technologies


Slide34 l.jpg

WeatherReport available

application

Mobile users

XSLT

WML

HTML

XML

PC users

Http://www.xweather.com

PowerWarning

application

XML

Email notifications

Registrations

Application

programs

XML

Internet Technologies


The xml describing the weather l.jpg
The XML Describing the Weather available

<?xml version="1.0" encoding="UTF-8"?>

<WeatherReport>

<City>Pittsburgh</City>

<State>PA</State>

<Date>Wed. April 11, 2001</Date>

<Time>3</Time>

<CurrTemp Unit = "Farenheit">70</CurrTemp>

<High Unit = "Farenheit">82</High>

<Low Unit = "Farenheit">62</Low>

</WeatherReport>

This file is behind

Jigsaw in the file

Www/weather/

weather.xml.

Perhaps this is

being served up

by www.xweather.com

for ½ cents per hit.

Internet Technologies


Serving the weather l.jpg
Serving the weather available

// This servlet file is stored in Www/Jigsaw/servlet/GetWeather.java

// This servlet returns a user selected xml weather file from

// the Www/weather directory and returns it to the client.

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class GetWeather extends HttpServlet {

This data would not

normally be retrieved

from a file.

It costs ½ cent per hit.

Internet Technologies


Slide37 l.jpg

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

String theData = "";

/* For simplicity we get the user’s request from the path. */

String extraPath = req.getPathInfo();

extraPath = extraPath.substring(1);

// read the file

try {

// open file and create a DataInputStream

FileInputStream theFile =

new FileInputStream("c:\\Jigsaw\\Jigsaw\\”+

“Jigsaw\\Www\\weather\\"+extraPath);

Internet Technologies


Slide38 l.jpg

InputStreamReader is = new InputStreamReader(theFile);

BufferedReader br = new BufferedReader(is);

// read the file into the string theData

String thisLine;

while((thisLine = br.readLine()) != null) {

theData += thisLine + "\n";

}

}

catch(Exception e) {

System.err.println("Error " + e);

}

PrintWriter out = res.getWriter();

out.write(theData);

System.out.println("Wrote document to client");

out.close();

}

}

Internet Technologies


Slide39 l.jpg

WeatherReport InputStreamReader(theFile);

application

Mobile users

XSLT

WML

HTML

XML

PC users

Http://www.xweather.com

PowerWarning

application

XML

Email notifications

Registrations

Application

programs

XML

Internet Technologies


Registrations html l.jpg
Registrations (HTML) InputStreamReader(theFile);

<!-- PowerWarningForm.html -->

<html>

<head>

<title>PowerWarning</title>

</head>

<body>

<form method="post" action="/servlet/PowerWarn">

E-Mail

<input type="text" name = "User"> <p>

State

<input type="text" name = "State"> <p>

City

<input type="text" name = "City"> <p>

Temperature

<input type="text" name = "Temperature"> <p>

Duration

<input type="text" name = "Duration"> <p>

<input type = "submit">

</form>

</body>

</html>

Internet Technologies


Registrations servlet l.jpg
Registrations (Servlet) InputStreamReader(theFile);

The servlet will create a watcher object for each registered

user. The watcher object will be told of each user’s location

and temperature requirements. Each watcher object will run

in its own thread and may or may not notify its assigned

user by email.

On servlet initialization, we will start up an object whose

responsibility it is to periodically wake up and tell the watcher

objects to check the weather.

Internet Technologies


Registrations servlet42 l.jpg
Registrations (Servlet) InputStreamReader(theFile);

/* This servlet is called by an HTML form. The form passes

the user email, state, city, temperature and duration.

*/

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class PowerWarn extends HttpServlet {

Internet Technologies


Slide43 l.jpg

static Hashtable userTable; InputStreamReader(theFile);/* Holds (email,watcher) pairs */

public void init(ServletConfig conf) throws ServletException {

super.init(conf);

PowerWarn.userTable = new Hashtable();

Scheduler scheduler = new Scheduler();

scheduler.start(); /* Run the scheduler */

} /* The scheduler can see the hash

table. It has package access.

*/

Internet Technologies


Slide44 l.jpg

public void doPost(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

/* Collect data from the HTML form */

String par_user = req.getParameter("User");

String par_state = req.getParameter("State");

String par_city = req.getParameter("City");

int par_temp = Integer.parseInt(

req.getParameter("Temperature"));

int par_duration = Integer.parseInt(

req.getParameter("Duration"));

Internet Technologies


Slide45 l.jpg

/* Assign a watcher to this user. */ HttpServletResponse res)

Watcher watcher = new Watcher(par_user, par_state, par_city,

par_temp, par_duration);

/* Place the (email,watcher) pair in the hash table. */

PowerWarn.userTable.put(par_user, watcher);

res.setContentType("text/html");

PrintWriter writer = res.getWriter();

writer.print("<html><head></head><body><b>” +

“You'll be notified by email</b></body></html>");

writer.close();

}

}

Internet Technologies


Slide46 l.jpg

Servlet HttpServletResponse res)

Http Request

PowerWarn.userTable

Watcher

[email protected]

User data

Email

Http Request

Watcher

User data

Email

[email protected]

Scheduler

Internet Technologies


The scheduler l.jpg
The Scheduler HttpServletResponse res)

import java.util.Enumeration;

public class Scheduler extends Thread {

public void run() {

System.out.println("Running scheduler");

while(true) {

Enumeration en = PowerWarn.userTable.elements();

while(en.hasMoreElements()) {

Watcher wa = (Watcher)en.nextElement();

new Thread(wa).start();

}

Internet Technologies


Slide48 l.jpg

try { /* put this thread to sleep for 15 seconds */

Thread.sleep(1000 * 15);

}

catch(InterruptedException ie) {

// ignore

}

} /* end while */

}

public Scheduler() {

super();

}

}

Fifteen seconds for

testing.

Internet Technologies


The watcher class l.jpg
The Watcher Class seconds */

The Watcher objects make HTTP requests to get XML.

How should we handle the XML? SAX or DOM?

SAX.

How do we send email?

JavaMail.

Internet Technologies


Slide50 l.jpg

import org.xml.sax.*; seconds */

import org.xml.sax.helpers.ParserFactory;

import java.io.*;

import java.net.*;

import org.w3c.dom.Document;

import javax.xml.parsers.SAXParserFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

Internet Technologies


Slide51 l.jpg

public class Watcher extends HandlerBase implements Runnable {

String user, state, city;

int temp, duration, overTemp;

public Watcher(String user, String state, String city, int temp,

int duration) {

super();

this.user = user;

this.state = state;

this.city = city;

this.temp = temp;

this.duration = duration;

this.overTemp = 0;

}

Internet Technologies


Slide52 l.jpg

public void run() { // called by scheduler {

System.out.println("Running watcher");

/* Set up to call the weather service. */

String weatheruri =

“http://mccarthy.heinz.cmu.edu:8001/servlet/GetWeather”+

“/weather.xml";

/* For simplicity we won’t take the appropriate approach. */

/* String weatheruri =

"http://mccarthy.heinz.cmu.edu:8001/servlet/GetWeather/?city="

+ URLEncoder.encode(this.city);

*/

/* Create an InputSource object for the parser to use. */

InputSource is = new InputSource(weatheruri);

Internet Technologies


Slide53 l.jpg

try { {/* Set up to handle incoming XML */

SAXParserFactory factory = SAXParserFactory.newInstance();

factory.setValidating(true);

SAXParser parser = factory.newSAXParser();

parser.parse(is, this); /* The parser makes the calls. */

}

catch(Exception e) {

e.printStackTrace();

return;

}

/* The parsing and callbacks are done by this time. */

int currentTempNumber;

try {

currentTempNumber = Integer.parseInt(this.currentTemp.trim());

}

catch( NumberFormatException e) {e.printStackTrace(); return; }

Internet Technologies


Slide54 l.jpg

/* See if the user wants to be alerted. */ {

if(currentTempNumber > this.temp) {

this.overTemp++;

if(this.overTemp >= this.duration) {

warning();

}

}

else {

this.overTemp = 0;

}

}

/* Send email via JavaMail. The Mailer class is based on the JavaMail API. */

public void warning() {

System.out.println("Sending email");

Mailer mailman = new Mailer(this.user,

"[email protected]", "It's hot");

mailman.send();

}

Internet Technologies


Slide55 l.jpg

/* Handle SAX events. */ {

StringBuffer buffer;

String currentTemp;

public void startDocument() throws SAXException {

this.currentTemp = null;

}

public void startElement(String name, AttributeList aMap) throws SAXException {

if(name.equals("CurrTemp")) { /* Prepare for next event. */

this.buffer = new StringBuffer();

}

}

Internet Technologies


Slide56 l.jpg

public void endElement(String name) throws SAXException { {

if(name.equals("CurrTemp")) {

this.currentTemp = this.buffer.toString();

this.buffer = null;

}

}

public void characters(char[] ch, int start, int length) throws SAXException {

if(this.buffer != null) this.buffer.append(ch,start,length);

}

}

Internet Technologies


Slide57 l.jpg

WeatherReport {

application

Mobile users

WML

HTML

XML

PC users

Http://www.xweather.com

PowerWarning

application

XML

Email notifications

Registrations

Application

programs

XML

Internet Technologies


Building a dom tree from scratch l.jpg
Building a DOM Tree From Scratch {

MyGradeBook.xml

<?xml version="1.0" encoding="UTF-8"?>

<GradeBook>

<Student>

<Score>100</Score>

</Student>

</GradeBook>

Let’s create this file

from within a java

program.

Internet Technologies


Slide59 l.jpg

// DomExample.java {

// Building an xml document from scratch

import java.io.*;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.*;

import com.sun.xml.tree.XmlDocument;

public class DomExample {

private Document document;

Internet Technologies


Slide60 l.jpg

// Get an empty Document object {

public DomExample () {

DocumentBuilderFactory factory =

DocumentBuilderFactory.newInstance();

try {

DocumentBuilder builder = factory.newDocumentBuilder();

document = builder.newDocument();

}

catch (Throwable t) {

t.printStackTrace ();

}

Internet Technologies


Slide61 l.jpg

// Ask the Document object for various types of nodes and {

// add them to the tree.

Element root = document.createElement("GradeBook");

document.appendChild(root);

Element student = document.createElement("Student");

root.appendChild(student);

Element score = document.createElement("Score");

student.appendChild(score);

Text value = document.createTextNode("100");

score.appendChild(value);

Internet Technologies


Slide62 l.jpg

// Write the Document to disk. {

try {

FileOutputStream fos = new

FileOutputStream("MyGradeBook.xml");

((XmlDocument)document).write(fos);

}

catch(IOException ioe) {

ioe.printStackTrace();

}

}

public static void main(String a[]) {

DomExample tree = new DomExample();

}

}

Internet Technologies


ad