Consequently, much of the remainder of this preface will consist of a chapter-by-chapter breakdown of these changes. 7.12 (formerly 7.11) to use the DataSource interface and create a DAO (this method has changed slightly since 2006), with consequent changes to the example code.
Clients, Servers and Peers
However, this chapter is intentionally short, as the author has avoided including material that is not relevant to using Java for network programming. The reader who already has a sound grasp of network concepts can skip this chapter entirely.
Basic Concepts, Protocols and Terminology
- Ports and Sockets
- The Internet and IP Addresses
- Internet Services, URLs and DNS
- The InetAddress Class
It is used to indicate one of the two endpoints of a communication link between two processes. Java is also referred to as 'the language of the Internet' and it is the use of Java in this context that has had a major influence on the popularity of the language.
Starting Network Programming in Java
Using Sockets
- TCP Sockets
- Datagram (UDP) Sockets
This is exactly the same as for the server process (using the method close to the Socket class. As was the case in the server code, there is no checked exception generated by the above close method in the finally clause of the client program, so there will be no try block.
Network Programming with GUIs
Some of the code for the client is also provided for you and is kept in file EmailClient.java, a printed version of which is also provided. Each of the above users has a message box on the server that can accept a maximum of 10 messages.
Multithreading and Multiplexing
Thread Basics
On computers, threads with the same priority each get an equal time slice or quantum of time for execution on the processor. For threads with the same priority, time-slicing is used, so that a thread does not have to wait for another thread with the same priority to finish.
Using Threads in Java
- Extending the Thread Class
- Explicitly Implementing the Runnable Interface
String(Runnable
Multithreaded Servers
Of course, for each client-handling thread that is created, the main thread must ensure that the client-handling thread is passed a reference to the socket opened for the associated client. When the ClientHandler thread is created, its constructor is provided with a reference to the relevant socket. Create a thread to handle communication with //this client and pass the constructor for this //thread a reference to the socket in question.
Locks and Deadlock
To avoid this problem in Java, we can request that a thread obtain a lock on the object to be updated. Here, thread1 has a lock on the res1 resource, but it needs to acquire a lock on res2 to complete its processing (so that it can release its lock on res1. At the same time, thread2 has a lock on res2, but it needs to acquire a lock on res1 in order to complete its processing.
Synchronising Threads
Method addOne of Resource will be called by a Producer object and will attempt to add one item to the resource level. When the resource level is below the maximum, addOne increases the level and then calls the notify method to 'wake up' any waiting ConsumerClient thread. When the resource level is above zero, takeOne decrements the level and then calls method notifyAll.
- Implementation
- Further Details
When this happens, the current SelectionKey must be unregistered from the Selector object. The (attempted) closure of the socket can fail and must be done inside a try block, but the example. The example above copies the entire contents of the buffer array and converts that copy to a string.
Serial Access Files
As soon as the program is finished, all data that has been entered and the results of the processing of such data are thrown away. The most common way to provide such persistent storage is to use disk files. Java provides such a facility where access to such files is either serial or random.
File Handling
File Methods
Redirection
Command Line Parameters
Suppose a compiled Java program called Copy.class copies the contents of one file to another. Please ignore the fact that MS-DOS has a perfect copy command that could do the job without using our Java program!). Method main would then access the filenames through arg[0] and arg[1] : import java.io.*;.
Random Access Files
Hence, the number of records in fi le ranAccts at any given time = ranAccts.length()/48. Write to file the first fixed-size characters of //string text, starting at byte zero. We should also be able to retrieve individual records from anywhere in the file and/or.
Serialisation [U.S. Spelling Seriali z ation]
The only useful option that seems to be is to catch the EOFException that is generated when we read past the end of the file. This example creates three objects of a class called Personnel and writes them to disk file (as objects). It then reads the three objects back from the file (using a typecast to convert them to their original type) and makes use of the 'get' methods of class staff to display the data members of the three objects.
File I/O with GUIs
The results of file keeping will be a simple serial file, with each student's data held in three fields in the following sequence: surname, first name(s) and exam mark. First we'll let the user navigate the computer's file system and select the desired file (using a JFileChooser object). Once a file has been selected, our program will open the file, read the first (logical) record, and display its contents within the text fields of a panel we set up.
ArrayLists
In common with the other member classes of Java's collection framework, the class of elements that can be held in an ArrayList structure is specified in angle brackets after the collection class name, both in the declaration of the ArrayList and in its creation. After executing the above rules, name1 and name2 will both refer to the string 'Jones'. An object can be added at a specific position within an ArrayList via an overloaded form of the add method that takes two arguments, the first of which specifies the position where the element should be added.
ArrayLists and Serialisation
The code for the server (PersonnelServer.java) and the client (PersonnelClient.java) is shown below. You'll find that the code for the server is an amalgamation of the first half of MessageServer.java from the chapter. The only new point worth mentioning in the code for the client is the necessary inclusion of ClassNotFoundException throws, both in the method that directly accesses the ArrayList of staff objects (run method) and in the method that calls this (main method).
Vectors Versus ArrayLists
Then use redirection to feed the values from your payroll text file into your program (displaying the content as before). After you compile and run the program, use the MS-DOS command type (or the equivalent command for your platform) to display the file's contents. The name and location of the file must be chosen by the user via a JFileChooser object.
The Basic RMI Process
However, in a distributed environment, it is often desirable to be able to invoke methods on remote objects (ie, objects located on other systems). Once a reference to a remote object is obtained, the methods of that object can be invoked in exactly the same way as methods of local objects. RMI has been a core component of Java since the earliest release of the language, but has undergone some evolutionary changes since its original specification.
Remote Method Invocation (RMI)
Implementation Details
Clients will then be able to use the name of the remote object to obtain a reference to that object via the registry. It does this by using the lookup method of the Naming class, passing as an argument to this method the same URL that the server did when it mapped the object reference to the object name in the registry. Get a reference to the object from the // registry and write it to the appropriate // type.
Compilation and Execution
This author, however, feels that separating the two probably results in a clearer delineation of responsibilities. Since the server process and the RMI registry will continue to run indefinitely after the client process has finished, they will need to be closed by pressing Ctrl-C in each of their windows. Now that the basic process is covered, the next section will consider a more realistic application of RMI.
Using RMI Meaningfully
This returns a reference to the ArrayList of Account objects which in turn provides access to the individual Account objects. The contents of the registry window will be identical to the screenshot shown in Fig. The server class creates an array of implementation objects and binds each individually to the registry.
RMI Security
This is accomplished by using the -D command line option to specify the codebase property setting. Access this implementation object through the client program and use the methods of the Result class to display the last name and exam grade for each of the two Result objects. Access these objects through the client program and use methods of the implementing class to display the last names and exam grades for each of the two objects.
Background and Basics
Although RMI is a powerful mechanism for distributing and processing objects in a platform-independent manner, it has a significant drawback - it only works with objects that are created using Java. A more general approach to developing distributed systems is provided by CORBA (Common Object Request Broker Architecture), which allows objects written in a variety of programming languages to be accessed by client programs that may themselves be written in a variety of languages. programming.
Chapter 6
CORBA
Java IDL includes an OMG-compatible version of IDL and the corresponding mapping from this IDL to Java. The module declaration starts with the keyword module followed by the name of the specific module (beginning with an uppercase letter, although the Java convention is that a package name begins with a lowercase letter). The body of the module declaration is surrounded by curly braces and, like C++ (but unlike Java), is terminated by a semicolon.
The Java IDL Process
This is done by calling the static method init of class ORB (from package org.omg.CORBA. This is accomplished by passing the above interface path to our naming context's resolve method, which returns a CORBA Object reference: org.omg.CORBA.Object objectRef =.vii) The 'narrow' interface reference. We 'downcast' the reference from the previous step to a Hello reference via static method narrower of the idlj-generated class HelloHelper.
Using Factory Objects
As in the first example, these classes will be placed inside the same file as our server code. In addition to the steps performed by the client in the previous example, the steps listed below will be performed. The above reference will be used to create a new StockItem object by calling the createStockItem method (giving the arguments required by the constructor).
Object Persistence
The ORB, name server, and object server program could have been started on one host, while the client (or clients) could have been started on another host (or hosts).
RMI-IIOP
Then use the number attribute 'set' method to change some numbers and display the table of results again. Create an array of Account objects and use the createAccount method of the AccountFactoryServant object to create the members of this array, using the data from the above three arrays in the construction of these members. Programs written in Java can communicate with relational databases (local or remote) through the Java Database Connectivity (JDBC) API, which became part of the core Java distribution with JDK 1.1.
Chapter 7
These days, of course, most organizations have structured the bulk of their data in databases, which often have to be accessed from more than one site.
Java Database Connectivity (JDBC)
- The Vendor Variation Problem
- SQL and Versions of JDBC
- Creating an ODBC Data Source
- Simple Database Access
The next section describes the process required to create an ODBC data source, with the next two sections describing the steps required to connect to the database and to retrieve or manipulate the contents of the database. The next section describes how our Java code can make use of the database's DSN to retrieve data from the database and is applicable to any type of relational database. Of course, if all fields in the database table have been selected by the query, then these two will be the same.