Java Reference
In-Depth Information
D option to set
a property value. For example, to set the property “user.favoritecolor” to “blue,” we would try
When running Java programs from the command line, we simply use the
% java −Duser.favoritecolor=blue ListProperties
Note that properties are typically defined with hierarchical (general to specific) names, such as
java.class.path . For brevity's sake, we did not use such a name, but in a production application
hierarchical names should be used to avoid collisions.
The main() of ThreadMain.java demonstrates how to use either the thread-per-client or
thread-pool server. This application takes three parameters: 1) the port number for the server,
2) the protocol name (use “Echo” for the echo protocol), and 3) the dispatcher name (use
“ThreadPer” or “Pool” for the thread-per-client and thread-pool servers, respectively). The
number of threads for the thread pool defaults to 8. However, this can be changed to 4, for
example, by setting a system property using the
DThreads=4 option to the JVM.
% java −DThreads=4 ThreadMain 5000 Echo Pool
Note that you must compile EchoProtocolFactory.java , ThreadPerDispatcher.java , and Pool-
Dispatcher.java explicitly before running ThreadMain.java . Failure to do so will result in a
ClassNotFoundException . Those classes are not referenced by name in ThreadMain (that's the
idea!), so they will not be automatically compiled with ThreadMain .
ThreadMain.java
0 import java.net.*; // for ServerSocket
1 import java.io.*;
// for IOException
2
3 public class ThreadMain {
4
5
public static void main(String[] args) throws Exception {
6
7
if (args.length != 3) // Test for correct # of args
8
throw new IllegalArgumentException("Parameter(s): [<Optional properties>]"
9
+ " <Port> <Protocol> <Dispatcher>");
10
11
int servPort = Integer.parseInt(args[0]); // Server Port
12
String protocolName = args[1];
// Protocol name
13
String dispatcherName = args[2];
// Dispatcher name
14
15
ServerSocket servSock = new ServerSocket(servPort);
16
Logger logger = new ConsoleLogger();
// Log messages to console
17
ProtocolFactory protoFactory = (ProtocolFactory) // Get protocol factory
18
Class.forName(protocolName + "ProtocolFactory").newInstance();
19
Dispatcher dispatcher = (Dispatcher)
// Get dispatcher
20
Class.forName(dispatcherName + "Dispatcher").newInstance();
21
 
Search WWH ::




Custom Search