Testing the launch of a module in a java ServerSocket? - java

I developed a Java ServerSocket that is always running and it executes many modules . My server start by execution a BatchScript file (start.bat). I want to test if the module is successfully executed and give the user w feedback on the cmd console(when i launch the server from eclipse i can see the long on eclipse console only) . I want to check my server statut after a periode of time knowing that it is always running .
Here is the implementation of my server :
public Server() throws InterruptedException
{
//this.es = e;
try {
sockserv= new ServerSocket(6020) ;
System.out.println("Server Started");
myModule mod= new myModule();
mod.displayLogOnCmd();
mod.checkServerEveryPeriodOfTime();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
#Override
public void run()
{
while(SERVER_STATUT)
{
try {
Socket socket = sockserv.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
My Question :
How to display log info of eclipse in the Cmd ? and how to see if the server launched the module or not ??
Thank You :)

Related

Why does my RMI server exit before a client can connect?

After starting the program to run on the specified port, the program terminates before a client is able to connect to it.
try {
StudentService obj = new StudentService();
Registry r = LocateRegistry.createRegistry(4200);
r.bind("localhost", obj);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This is the is the method that is located in the StudentService class that has been overridden.
int multiply(int s, int b) throws RemoteException {
return s * b;
}
From the documentation,
UnicastRemoteObject.exportObject exports the supplied remote object to receive incoming remote method invocations on an anonymous TCP port and returns the stub for the remote object to pass to clients
So, there is no surprise your program ended without listening for incoming connections. This should work.
try {
StudentService obj = new StudentService();
UnicastRemoteObject.exportObject(obj, 0);
Registry r = LocateRegistry.createRegistry(4200);
r.bind("localhost", obj);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

How to ship android app with R script in it and make sure it executes

I am building one Android machine learning app where in a java class I need to make a call to a R script. But before that I need to make sure that R is up and running. How do I do that? How to get the Rconnection established in android code?
public String getSum() {
RConnection connection = null;
try {
/* Create a connection to Rserve instance running on default port
* 6311
*/
connection = new RConnection();
connection.eval("library(randomForest)");
/* Note four slashes (\\\\) in the path */
connection.eval("source('MyScript.R')");
REXP sum =connection.eval("predictSAL()");
this.sum = sum.asString();
} catch (RserveException e) {
e.printStackTrace();
} catch (REXPMismatchException e) {
e.printStackTrace();
} catch (REngineException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sum;
}

Using Desktop.browse doesn't allow program to exit automatically

If I make a simple program that browses to google.com, the program doesn't exit automatically when reaching the end of the main method. I would rather not call System.exit(0), so I was wondering if there was a way to "close" the desktop thread so it can exit automagically.
public static void main(String[] args) {
try {
Desktop.getDesktop().browse(new URI("http://google.com"));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}

Refresh the workspace after file creation through java code

I am creating java files from json Objects using a library called jsonschema2pojo-core.jar. It successfully creates the required files for me. Now I need to access the newly(dynamically) created file and creates its instance to use it further.
But as the newly created class is still not in the classpath I am unable to do this. Tried to do my part of research and figured out that Eclipse jars allows such refresh only in plugin projects. Can anyone suggest some thing for this?
public static void main(String[] args){
String fileName = "MyJavaFile";
POJOBuilder pojo = new POJOBuilder();
pojo.buildPOJO("file:///C:/mypath/myJSON.json", fileName); //generates the java file MyJavaFile.java
Object obj = null;
try {
obj = Class.forName("com.mypackage."+fileName).newInstance(); // Java file not available yet
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Can this be done through threads? I mean wait until the creation of the POJO is done and then start with the rest after that?

Asmack set up for Android

I want to create a messaging application for Android, so I've been reading up about XMPP and Asmack.
Asmack sounds pretty much exactly what I want, especially after reading this other question - Android and XMPP: Currently available solutions.
However, I'm fairly new to Android programming and also Github.
I have found the src for Asmack at https://github.com/Flowdalic/asmack, and also downloaded the jar from http://asmack.freakempire.de/4.0.4/, which I have included in my project. However I am unsure how the folders in the github asmack folder are supposed to be used. After hours trawling google trying to find some sort of step by step guide on how to set aSmack up, and not finding anything useful, I'm losing the will to live!
I'm using the following example code I found on the Smack github page, and I dont get any errors:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XMPPTCPConnection connection = new XMPPTCPConnection("jabber.org");
try {
connection.connect();
connection.login("mtucker", "password");
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Chat chat = ChatManager.getInstanceFor(connection)
.createChat("jsmith#jivesoftware.com", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});
try {
chat.sendMessage("Howdy!");
} catch (NotConnectedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
However I haven't used the 'build environment' from aSmack github repository - how is this meant to be included/used?
Thanks for any help!
You need to add single Jar file (last version now is 4.0.6) to your library and also don't forget to set proper permissions in manifest file.

Categories

Resources