example/JAVA/Makefile.am
RULE_JAVA_EXE = $(JAVAC) -classpath "$(top_builddir)$(FILE_SEP)javamsgque$(FILE_SEP)javamsgque.jar$(PATH_SEP)." $(JAVA_DEBUG) -d . -Xlint:unchecked "$<"
BUILT_SOURCES += example/MyClient.class
example/MyClient.class: $(srcdir)/MyClient.java ../../javamsgque/javamsgque.jar
$(RULE_JAVA_EXE)
BUILT_SOURCES += example/MyServer.class
example/MyServer.class: $(srcdir)/MyServer.java ../../javamsgque/javamsgque.jar
$(RULE_JAVA_EXE)
win.example/JAVA/MyServer.java
package example; import javamsgque.*; final class MyServer extends MqS implements IServerSetup, IFactory { // service to serve all incomming requests for token "HLWO" class MyFirstService implements IService { public void Service (MqS ctx) throws MqSException { SendSTART(); SendC("Hello World"); SendRETURN(); } } // define a service as link between the token "HLWO" and the class "MyFirstService" public void ServerSetup() throws MqSException { ServiceCreate("HLWO", new MyFirstService()); } // interface to create a new instance public MqS Factory() { return new MyServer(); } public static void main(String[] argv) { MqS.Init("java", "example.MyServer"); MyServer srv = new MyServer(); try { srv.LinkCreate(argv); srv.ProcessEvent(MqS.WAIT.FOREVER); } catch (Throwable e) { srv.ErrorSet(e); } srv.Exit(); } }
The server is started as network visible TCP server listen on PORT 2345 using a THREAD for every new connection request:
> java example.MyServer --tcp --port 2345 --thread
If you are using UNIX and if you want to setup a high-performance local server then use the build-in UDS (Unix-Domain-Sockets) capability to listen on the FILE /path/to/any/file.uds instead on a network port:
> java example.MyServer --uds --file /path/to/any/file.uds --thread
Three things are important:
Services are created with the ctx.ServiceCreate(String token, IService service) function. The first parameter is a 4 byte Token as public name. 4 byte is required because this string is mapped to a 4 byte integer for speed reason. The second parameter is an object providing the SERVICE CALLBACK interface.
The SERVICE CALLBACK function has one parameter of type MqS. This parameter refer to the original object (in our case MyServer). If the service object (in our case MyFirstService) is a subclass of the original MqS object class, the both statements SendSTART() and ctx.sendSTART() are identical.
package example; import javamsgque.*; class MyClient extends MqS { public static void main(String[] argv) { MyClient c = new MyClient(); try { c.LinkCreate(argv); c.SendSTART(); c.SendEND_AND_WAIT("HLWO", 5); System.out.println(c.ReadC()); } catch (Throwable e) { c.ErrorSet(e); } c.Exit(); } }
To call a network visible TCP server listen on PORT 2345 use:
> java example.MyClient --tcp --port 2345 > Hello World
To call a network visible UDP server listen on FILE /path/to/any/file.uds use:
> java example.MyClient --udp --file /path/to/any/file.uds > Hello World
To call a local server started by the client using PIPE communication use:
> java example.MyClient @ java example.MyServer > Hello World
1.5.8