楼主: ReneeBK
1125 6

[读者文库]Java Distributed Computing [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4897份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49635 个
通用积分
55.6937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2015-6-6 09:13:43 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


  • Java Distributed Computing
  • By: Jim Farley

  • Publisher: O'Reilly Media, Inc.

  • Pub. Date: January 1, 1998

  • Print ISBN-13: 978-1-56592-206-8

  • Pages in Print Edition: 386

  • Subscriber Rating: [0 Ratings]



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:distributed computing tribute Comput comp Media Java

本帖被以下文库推荐

沙发
ReneeBK 发表于 2015-6-6 09:14:33
  1. Example 2-2. An InputStream Subclass for Reading XDR-Formatted Data
  2. package dcj.examples;

  3. import java.io.*;
  4. import java.net.*;

  5. class XDRInputStream extends FilterInputStream {
  6.   public XDRInputStream(InputStream in) {
  7.     super(in);
  8.   }

  9.   // Overridden methods from FilterInputStream, implemented
  10.   // to read XDR-formatted data

  11.   public boolean readBoolean() throws IOException;
  12.   public byte    readByte() throws IOException;
  13.   public int     readUnsignedByte() thows IOException;
  14.   public float   readFloat() thows IOException;
  15.   // Other readXXX() methods omitted in this example...

  16.   // We'll assume this stream doesn't support mark/reset operations

  17.   public boolean markSupported() { return false; }
复制代码

藤椅
ReneeBK 发表于 2015-6-6 09:15:10
  1. Example 2-3. Example XDRInputStream Client
  2. import dcj.examples.XDRInputStream;
  3. import java.io.*;

  4. class XDRInputExample
  5. {
  6.   public static void main(String argv[])
  7.     {
  8.       String host = argv[0];

  9.       // Default port is 5001
  10.       int port = 5001;

  11.       try
  12.         {
  13.           port = Integer.parseInt(argv[1]);
  14.         }
  15.       catch (NumberFormatException e)
  16.         {
  17.           System.out.println("Bad port number given, using default "
  18.                              + port);
  19.         }

  20.       // Try connecting to specified host and port
  21.       Socket serverConn = null;
  22.       try { serverConn = new Socket(host, port); }
  23.       catch (UnknownHostException e)
  24.         {
  25.           System.out.println("Bad host name given.");
  26.           System.exit(1);
  27.         }

  28.       // Wrap an XDR stream around the input stream
  29.       XDRInputStream xin = new XDRInputStream(serverConn.getInputStream());

  30.       // Start reading expected data from XDR-formatted stream
  31.       int numVals = xin.readInt();
  32.       float val1 = xin.readFloat();
  33.       ...
  34.     }
  35. }
复制代码

板凳
ReneeBK 发表于 2015-6-6 09:17:01
  1. Example 7-2. Base Class for Database Items
  2. package dcj.examples.dbase;

  3. import java.sql.*;

  4. abstract class DatabaseItem {
  5.   static Connection dbConn;
  6.   boolean valid;

  7.   public boolean isValid() { return valid; }

  8.   public abstract boolean updateToDbase();
  9.   public abstract boolean updateFromDbase();
  10. }
复制代码
Example 7.2 shows the Java code for the DatabaseItem interface. This base class provides slots for a global java.sql.Connection object and for a validity flag. The global Connection is used for all transactions made by the data objects with the database to retrieve or update their data. The DatabaseItem interface also includes two abstract methods: updateToDbase() and updateFrom-Dbase(). These have to be implemented in subclasses to perform the necessary database transactions to store the object’s data to the database, and to retrieve the object’s current data from the database, respectively. Anytime during the lifetime of a DatabaseItem, its updateFromDbase() method can be called to refresh the local data from the database, or its updateToDbase() method can be called to update the database with the local data.

报纸
ReneeBK 发表于 2015-6-6 09:17:59
  1. Example 7-3. Schedule Resource Object
  2. package dcj.examples.dbase;

  3. import java.sql.*;
  4. import java.util.Vector;

  5. class SchedResource extends DatabaseItem {
  6.   int rid;
  7.   String name;
  8.   int type;
  9.   float size;

  10.   public SchedResource(int id) {
  11.     rid= id;
  12.     valid= updateFromDbase();
  13.   }

  14.   public SchedResource(String n, int t, float s) {

  15.     try {
  16.       Statement st = DatabaseItem.dbConn.createStatement();
  17.       int rcnt = st.executeUpdate("INSERT INTO resource "
  18.                                   + "(rid, name, type, size) VALUES "
  19.                                   + "(ridseq.nextval, " + n + ", "
  20.                                   + t + ", " + s + ")");
  21.       if (rcnt == 1)
  22.         valid= true;
  23.       else
  24.         valid= false;
  25.     }
  26.     catch (SQLException e) {
  27.       valid= false;
  28.     }
  29.   }

  30.   public int    getId()           { return rid; }

  31.   public String getName()         { return name; }
  32.   public void   setName(String n) { name = n; updateToDbase(); }

  33.   public int    getType()         { return type; }
  34.   public void   setType(int t)    { type = t; updateToDbase(); }

  35.   public float  getSize()         { return size; }
  36.   public void   setSize(float s)  { size = s; updateToDbase(); }

  37.   public boolean updateFromDbase() {
  38.     boolean success = true;

  39.     try {
  40.       Statement s = DatabaseItem.dbConn.createStatement();
  41.       ResultSet r =
  42.         s.executeQuery("SELECT name, type, size FROM resource WHERE rid= "
  43.                        + rid);
  44.       if (r.next()) {
  45.         name = r.getString("name");
  46.         type = r.getInt("type");
  47.         size = r.getFloat("size");
  48.       }
  49.       else {
  50.         success = false;
  51.       }

  52.       s.close();
  53.     }
  54.     catch (SQLException e) {
  55.       success = false;
  56.     }

  57.     return success;
  58.   }

  59.   public boolean updateToDbase() {
  60.     boolean success = true;

  61.     try {
  62.       Statement s = DatabaseItem.dbConn.createStatement();
  63.       int numr = s.executeUpdate("UPDATE resource SET name = " + name
  64.                                  + ", type = " + type + ", size = " + size
  65.                                  + " WHERE rid= " + rid);
  66.       if (numr < 1) {
  67.         success = false;
  68.       }
  69.     }
  70.     catch (SQLException s) {
  71.       success = false;
  72.     }

  73.     return success;
  74.   }

  75.   static public Vector getAllResources() {
  76.     Vector resList = new Vector();
  77.     try {
  78.       Statement s = DatabaseItem.dbConn.createStatement();
  79.       ResultSet r = s.executeQuery("SELECT distinct(rid) FROM resource");
  80.       while (r.next()) {
  81.         int id= r.getInt("rid");
  82.         SchedResource res = new SchedResource(id);
  83.         if (res.isValid()) {
  84.           resList.addElement(res);
  85.         }
  86.       }
  87.     }
  88.     catch (Exception e) {};

  89.     return resList;
  90.   }
  91. }
复制代码

地板
ReneeBK 发表于 2015-6-6 09:18:40
  1. Example 7-4. Schedule Task Object
  2. package dcj.examples.dbase;

  3. import java.sql.*;
  4. import java.util.Vector;

  5. class SchedTask extends DatabaseItem {
  6.   int tid;
  7.   int type;
  8.   float size;

  9.   SchedTask(int id) {
  10.     tid= id;
  11.     valid= updateFromDbase();
  12.   }

  13.   SchedTask(int t, float sz) {
  14.     type = t;
  15.     size = sz;
  16.     try {
  17.       Statement s = DatabaseItem.dbConn.createStatement();
  18.       int cnt = s.executeUpdate("INSERT INTO task (tid, type, size) "
  19.                                 + "VALUES (tidseq.nextval, " + type + ", "
  20.                                 + size + ")");
  21.       if (cnt < 1) {
  22.         valid= false;
  23.       }
  24.       else {
  25.         valid= true;
  26.       }
  27.     }
  28.     catch (SQLException e) {
  29.       valid= false;
  30.     }
  31.   }

  32.   public int    getId()           { return tid; }

  33.   public int    getType()         { return type; }
  34.   public void   setType(int t)    { type = t; }

  35.   public float  getSize()         { return size; }
  36.   public void   setSize(float s)  { size = s; }

  37.   public boolean updateFromDbase() {
  38.     boolean success = true;

  39.     try {
  40.       Statement s = DatabaseItem.dbConn.createStatement();
  41.       ResultSet r =
  42.         s.executeQuery("SELECT type, size FROM task WHERE tid= "
  43.                        + tid);
  44.       if (r.next()) {
  45.         type = r.getInt("type");
  46.         size = r.getFloat("size");
  47.       }
  48.       else {
  49.         success = false;
  50.       }

  51.       s.close();
  52.     }
  53.     catch (SQLException e) {
  54.       success = false;
  55.     }

  56.     return success;
  57.   }

  58.   public boolean updateToDbase() {
  59.     boolean success = true;

  60.     try {
  61.       Statement s = DatabaseItem.dbConn.createStatement();
  62.       int numr = s.executeUpdate("UPDATE task SET type = "
  63.                                  + type + ", size = " + size
  64.                                  + " WHERE tid= " + tid);
  65.       if (numr < 1) {
  66.         success = false;
  67.       }
  68.     }
  69.     catch (SQLException s) {
  70.       success = false;
  71.     }

  72.     return success;
  73.   }

  74.   static public Vector getAllTasks() {
  75.     Vector taskList = new Vector();

  76.     try {
  77.       Statement s = DatabaseItem.dbConn.createStatement();
  78.       ResultSet r = s.executeQuery("SELECT distinct(tid) FROM task");
  79.       while (r.next()) {
  80.         int id= r.getInt("tid");
  81.         SchedTask task = new SchedTask(id);
  82.         if (task.isValid()) {
  83.           taskList.addElement(task);
  84.         }
  85.       }
  86.     }
  87.     catch (Exception e) {}

  88.     return taskList;
  89.   }
  90. }
复制代码

7
ReneeBK 发表于 2015-6-6 09:19:36
  1. Example 7-5. Time Constraint Object
  2. package dcj.examples.dbase;

  3. import java.sql.*;
  4. import java.util.Vector;

  5. class TimeConstraint extends DatabaseItem {
  6.   int ctype;
  7.   int task1;
  8.   int task2;

  9.   // This constructor is used to create a representation
  10.   // of a constraint in the database.
  11.   public TimeConstraint(int type, int tid1, int tid2,
  12.                         boolean insert) {
  13.     ctype = type;
  14.     task1 = tid1;
  15.     task2 = tid2;

  16.     if (insert) {
  17.       // Create a new record in the database.
  18.       try {
  19.         Statement s = DatabaseItem.dbConn.createStatement();
  20.         int numr = s.executeUpdate("INSERT INTO time_constraint "
  21.                                + "(type, task1, task2) VALUES ("
  22.                                + type + ", " + task1 + ", " + task2 + ")");
  23.         if (numr != 1)
  24.           valid= false;
  25.         else
  26.           valid= true;
  27.       }
  28.       catch (SQLException e) {
  29.         valid= false;
  30.       }
  31.     }
  32.   }

  33.   public int getTask1Id() { return task1; }
  34.   public int getTask2Id() { return task2; }
  35.   public int getType() { return ctype; }

  36.   static public Vector constraintsFor(int tid) {
  37.     Vector constraints = new Vector();

  38.     try {
  39.       Statement s = DatabaseItem.dbConn.createStatement();
  40.       ResultSet r = s.executeQuery("SELECT task1, task2, type FROM "
  41.                                    + "time_constraint where task1 = "
  42.                                    + tid + " or task2 = " + tid);
  43.       while (r.next()) {
  44.         int tid1 = r.getInt("task1");
  45.         int tid2 = r.getInt("task2");
  46.         int type = r.getInt("type");
  47.         TimeConstraint c = new TimeConstraint(type, tid1, tid2,
  48.                                               false);
  49.         constraints.addElement(c);
  50.       }
  51.     }
  52.     catch (Exception e) {}

  53.     return constraints;
  54.   }

  55.   // This class represents non-indexed table data, so we can't
  56.   // load or update one uniquely from the database
  57.   public boolean updateFromDbase() { return false; }
  58.   public boolean updateToDbase() { return false; }
  59. }
  60. Example 7-6. Resource Assignment Object
  61. package dcj.examples.dbase;

  62. import java.sql.*;
  63. import java.util.Vector;
  64. import java.util.Date;

  65. class ResAssignment extends DatabaseItem {
  66.   int rid;
  67.   int tid;
  68.   Date timestamp;

  69.   ResAssignment(int res, int task, Date time, boolean insert) {
  70.     rid= res;
  71.     tid= task;
  72.     timestamp = time;

  73.     if (insert) {
  74.       // Create a new record in the database.
  75.       try {
  76.         Statement s = DatabaseItem.dbConn.createStatement();
  77.         int numr = s.executeUpdate("INSERT INTO res_assignment "
  78.                      + " (resource, task, time) VALUES ("
  79.                      + rid + ", " + tid + ", " + time + ")");
  80.         if (numr != 1)
  81.           valid= false;
  82.         else
  83.           valid= true;
  84.       }
  85.       catch (SQLException e) {
  86.         valid= false;
  87.       }
  88.     }
  89.   }

  90.   public int  getResourceId() { return rid; }
  91.   public int  getTaskId() { return tid; }
  92.   public Date getTimeStamp() { return timestamp; }

  93.   static public Vector assignmentsFor(int rid) {
  94.     Vector ras = new Vector();

  95.     try {
  96.       Statement s = DatabaseItem.dbConn.createStatement();
  97.       ResultSet r = s.executeQuery("SELECT task, time FROM "
  98.                                    + "res_assignment where resource = "
  99.                                    + rid);
  100.       while (r.next()) {
  101.         int tid= r.getInt("task");
  102.         Date time = r.getDate("time");
  103.         ResAssignment ra = new ResAssignment(rid, tid, time, false);
  104.         ras.addElement(ra);
  105.       }
  106.     }
  107.     catch (Exception e) {}

  108.     return ras;
  109.   }

  110.   // This class represents non-indexed table data, so we can't
  111.   // load or update one uniquely from the database
  112.   public boolean updateFromDbase() { return false; }
  113.   public boolean updateToDbase() { return false; }
  114. }
复制代码

With these data objects defined, we can now use them in several distributed application contexts to access local and remote schedule databases. If the schedule database is local, then we simply need to have the JDBC drivers for the DBMS on our class path when we run an application built against these objects. The application will create a java.sql.Connection object with the name of the local database and use it to initialize all schedule-related objects. If we want to put the database on a remote server, then we have two ways to connect these data objects to the database. If the native DBMS interface provides a network interface and if the JDBC drivers we are using support this vendor-specific network interface, then we can install the DBMS network interface and modify the URL used to create the JDBC connection to the database to reflect the remote location. If this is not an option, either because the DBMS has no network interface, or because we cannot use the DBMS network interface on the client machine for some reason, then we have the option of using JDBC drivers with their own network protocol (as we discussed in an earlier section). The database host machine would need to have the corresponding server drivers installed, including the drivers that speak the native DBMS interface protocol.

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-27 01:09