I made an email client which has two main threads: the first one is the one with the GUI and the second one is that which get executed in the background in a loop and that update the GUI if their new emails. I would like to synchronize these this thread and execute them one at a time. On the server I manage them on this way:
public void initModel() throws IOException {
contenutoTextArea.append("Waiting for connections\n");
textarea.setText(contenutoTextArea.toString());
s = new ServerSocket(5000);
new Thread() {
#Override
public void run() {
while (true) {
try {
new ThreadedEchoHandler(s);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}.start();
}
class ThreadedEchoHandler implements Runnable {
private Socket incoming;
private String nomeAccount = "";
ThreadedEchoHandler(ServerSocket serv) throws IOException {
incoming = serv.accept();
new Thread(this).start();
}
public void run() {
....
}
Does the JVM execute them on in time?
I'm plannning a class which dynamically opens a connection to a database to store some settings.
The class should automatically close the connection if not used for a specific time or if the calling method ends.
Here is my solution so far, only showing the relevant parts:
public class DBSettings {
// ...
private int ConnectionTimeout = 5000;
private ExecutorService Executor;
private long LastTimeUsed;
// ...
public DBSettings(DBConnection Conn) {
Connection = new DBConnection(); // class handles Connection to Database
// ...
}
private void connect() {
try {
if (!Connection.isOpen()) {
Connection.openConnection(DBUrl, DBUserName, DBPassword);
LastTimeUsed = System.currentTimeMillis();
Executor = Executors.newSingleThreadExecutor();
Runnable closeRunnable = new Runnable() {
#Override
public void run() {
while (System.currentTimeMillis() < (LastTimeUsed + ConnectionTimeout)) {
try {
Thread.sleep(500);
} catch (InterruptedException e) { }
}
disconnect();
}
};
Executor.submit(closeRunnable);
}
} catch (Exception e) { // ... }
}
private void disconnect() {
if (Connection!=null) {
if (Executor!=null) {
Executor.shutdown();
}
Connection.closeConnection();
}
}
public void setValue(String group, String key, String value) {
// ...
LastTimeUsed = System.currentTimeMillis();
}
}
ExecutorService stops fine after 5 seconds and the connection closes.
But unfortunately keeps running for at least 5 seconds if the caller-method ends.
My test program:
private static void testDBSettings(DBConnection Conn) {
// using my class
DBSettings settings = new DBSettings(Conn);
// set first value, open connection
settings.setValue("Groupname", "Keyname", "Value");
Thread.sleep(1000);
// set second value, extend connection lifetime
settings.setValue("otherGroupname", "otherKeyname", "otherValue");
Thread.sleep(1000);
// here is the problem: after "settings" goes out of scope my class should
// stop or terminate the ExecutorService without the need to call an extra method.
}
I read a lot about Threads but couldn't find a solution.
tried another approach with ScheduledExecutorService - same result.
finalize() doesn't work because it is only called by garbage colletion.
Can anyone help me?
There is no way to track this in java.
Closest that you can do is use try-with-resource
try(MyClass myClass = new MyClass()){
myClass.doSomething();
} // Here myClass.close(); will be called.
I am looking for a way to connect to multiple instances of Glassfish 4+ (JDK7-EE) simultaneously from a stand-alone Swing-based client (JDK7-SE). I successfully connect to a single instance by the following way:
That's the construction of the initial context:
private void connect(String address, String port) {
System.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
System.setProperty("com.sun.corba.ee.transport.ORBTCPTimeouts", "500:30000:20:"+Integer.MAX_VALUE);
System.setProperty("com.sun.corba.ee.transport.ORBTCPConnectTimeouts", "250:90000:100:"+Integer.MAX_VALUE);
System.setProperty("com.sun.corba.ee.transport.ORBWaitForResponseTimeout", "300000");
System.setProperty("java.security.auth.login.config", new File("login.conf").getAbsolutePath());
System.setProperty("org.omg.CORBA.ORBInitialHost", address);
System.setProperty("org.omg.CORBA.ORBInitialPort", port);
InitialContext context = new InitialContext();
}
Look-ups are done by JNDI using a remote interface:
context.lookup("java:global/LawSuiteEE/LawSuiteEE-ejb/GlobalsFacade!ch.lawsuite.control.GlobalsFacadeRemote");
I am using a custom JDBC realm that resides on the server and works fine. On the client side I pass the following login.conf to the initial context (see code above):
default {
com.sun.enterprise.security.auth.login.ClientPasswordLoginModule required debug=true;
};
Authentication is currently done by ProgrammaticLogin:
private void login(String username, char[] password) {
ProgrammaticLogin plogin = new ProgrammaticLogin();
plogin.login(username, password);
}
All of this is working fine! But during startup of the stand-alone client, I want to simultaneously connect to another EJB located on a different server.
Since ProgrammaticLogin has no direct relation to the initial context, I am not sure how to login to two different Glassfish servers simulteneously with different credentials (e.g. username/password) ? Someone any ideas ?
Further examination of the issue has uncovered, that the initial context can only be set once on a per JVM basis. So as soon as the ORB is set up by using System.setProperty(String, String) and the inital context object is instantiated, the design of the SerialInitContextFactory let's you no more change the selected endpoint(s).
Therefore I decide to connect within different JVMs to the different Glassfish servers. So finally I ended up with a separate project that manages the connections to the application server and communicates by RMI with the main project.
Currently my project consists of two different EE projects to which I want connect simultaneously, namely "LawSuiteEE" and "MgmtCenterEE". Here's the new project that handles the connections:
public static void main(String args[]) {
try {
if(args.length==2) {
if(args[1].equals("LawSuiteEE")) {
ILawSuiteEE stub = (ILawSuiteEE) UnicastRemoteObject.exportObject(new LawSuiteEE(), 0);
Registry registry = LocateRegistry.createRegistry(Integer.parseInt(args[0]));
registry.bind("LawSuiteEE", stub);
} else if(args[1].equals("MgmtCenterEE")) {
ILawSuiteEE stub = (ILawSuiteEE) UnicastRemoteObject.exportObject(new MgmtCenterEE(), 0);
Registry registry = LocateRegistry.createRegistry(Integer.parseInt(args[0]));
registry.bind("MgmtCenterEE", stub);
} else {
throw new NumberFormatException();
}
Logger.getLogger(RemoteContext.class.getName()).log(Level.INFO, "Remote context service is listening on port "+args[0]+" for incoming requests delegating to "+args[1]+".");
System.out.println("SIGNAL[READY]");
} else {
throw new NumberFormatException();
}
} catch (RemoteException ex) {
System.exit(1);
} catch (AlreadyBoundException ex) {
System.exit(2);
} catch(NumberFormatException ex) {
System.exit(3);
}
The interface ILawSuiteEE is used for RMI between this and the main project (the second interface IMgmtCenterEE is quite the same):
public interface ILawSuiteEE extends IConcurrentDatastore {
void connect(String address, String port) throws RemoteException;
void disconnect() throws RemoteException;
boolean login(String username, char[] password) throws RemoteException;
}
The appropriate implementation:
public class LawSuiteEE implements ILawSuiteEE {
private InitialContext context;
private ProgrammaticLogin login;
#Override
public void connect(String address, String port) throws RemoteException {
if(context==null) {
try {
System.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
System.setProperty("com.sun.corba.ee.transport.ORBTCPTimeouts", "500:30000:20:"+Integer.MAX_VALUE);
System.setProperty("com.sun.corba.ee.transport.ORBTCPConnectTimeouts", "250:90000:100:"+Integer.MAX_VALUE);
System.setProperty("com.sun.corba.ee.transport.ORBWaitForResponseTimeout", "300000");
System.setProperty("java.security.auth.login.config", new File("login.conf").getAbsolutePath());
System.setProperty("org.omg.CORBA.ORBInitialHost", address);
System.setProperty("org.omg.CORBA.ORBInitialPort", Integer.toString(port));
Logger.getLogger(RemoteDatastore.class.getName()).log(Level.INFO, "Try to connect to application server at "+System.getProperty("org.omg.CORBA.ORBInitialHost")+":"+System.getProperty("org.omg.CORBA.ORBInitialPort")+" ...");
context = new InitialContext();
} catch (NamingException ex) {
throw new RemoteException(ex.getMessage());
}
}
}
#Override
public void disconnect() throws RemoteException {
if(context!=null) {
try {
context.close();
Logger.getLogger(LawSuiteEE.class.getName()).log(Level.INFO, "Server context successfully closed.");
} catch (NamingException ex) {
Logger.getLogger(LawSuiteEE.class.getName()).log(Level.SEVERE, "Couldn't close server context.");
} finally {
this.facades.clear();
this.services.clear();
this.context=null;
}
}
}
#Override
public boolean login(String username, char[] password) throws RemoteException {
login = new ProgrammaticLogin();
return login.login(username, password);
}
}
In the main project I'm going to connect with the following:
public class LawSuiteDatastore extends Thread implements ILawSuiteEE {
private int port;
private int trials;
private boolean ready;
private Process process;
private ILawSuiteEE stub;
public LawSuiteDatastore() {
this.setName("K+: Remote-Datastore-Connection");
this.port = RemoteDatastoreService.cport++;
}
#Override
public void run() {
try {
Tools.log(RemoteDatastoreService.class, Level.INFO, "Starting RMI registry on port "+port+" for connecting to LawSuiteEE server instance.");
this.process = Runtime.getRuntime().exec(new String[] {"java", "-jar", Context.getWorkingDirectory()+"/lib/LawSuiteSX.jar", Integer.toString(port), "LawSuiteEE"});
//<editor-fold defaultstate="collapsed" desc="Redirect Error Stream">
new Thread(new Runnable() {
#Override
public void run() {
try{
try(DataInputStream in = new DataInputStream(process.getErrorStream())) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while((line=br.readLine())!=null) {
Tools.log(RemoteDatastoreService.class, Level.SEVERE, line);
}
}
} catch(Exception ex){
Tools.log(MgmtCenterDatastore.class, Level.SEVERE, ex.getMessage());
}
}
}).start();
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Redirect Output Stream">
new Thread(new Runnable() {
#Override
public void run() {
try{
try(DataInputStream in = new DataInputStream(process.getInputStream())) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while((line=br.readLine())!=null) {
if(line.contains("SIGNAL[READY]")) { ready=true; }
Tools.log(RemoteDatastoreService.class, Level.INFO, line);
}
}
} catch(Exception ex){
Tools.log(MgmtCenterDatastore.class, Level.SEVERE, ex.getMessage());
}
}
}).start();
//</editor-fold>
// keep thread alive as long process is alive
if(process.waitFor()>0) {
// port was already bound
if(process.exitValue()==2) {
// try it with a different port and start over again
if(trials<3) {
process = null;
port = ++RemoteDatastoreService.cport;
trials++;
if(trials<3) {
start();
}
}
}
}
} catch (IOException ex) {
Tools.log(RemoteDatastoreService.class, Level.SEVERE, ex.getMessage());
} catch (InterruptedException ex) {
Tools.log(RemoteDatastoreService.class, Level.SEVERE, ex.getMessage());
}
}
public boolean isReady() {
return ready;
}
public int getTrials() {
return trials;
}
#Override
public void connect(RemoteDatastore datastore) throws RemoteException {
try {
Tools.log(RemoteDatastoreService.class, Level.INFO, "Locating RMI registry on port "+port+" for connecting to LawSuiteEE server instance.");
Registry registry = LocateRegistry.getRegistry(port);
stub = (ILawSuiteEE)registry.lookup("LawSuiteEE");
stub.connect(datastore);
} catch (NotBoundException ex) {
Logger.getLogger(RemoteDatastoreService.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void disconnect() throws RemoteException {
if(process!=null && stub!=null) {
stub.disconnect();
process.destroy();
} else {
throw new RemoteException("Remote RMI server is not ready.");
}
}
#Override
public boolean login(String username, char[] password) throws RemoteException {
if(process!=null && stub!=null) {
return stub.login(username, password);
} else {
throw new RemoteException("Remote RMI server is not ready.");
}
}
}
How about using multiple threads, one for each server?
You can create a new thread for each connection you need, set up the InitialContext on each thread and connect with the ProgrammaticLogin with different credentials.
You can create your own "custom" thread by implementing the Runnable interface, and create a constructor for it that receives the credentials and/or InitialContext object.
Simple example:
public class MyThread implements Runnable {
private ProgrammaticLogin plogin;
private string user;
private char[] pass;
public MyThread(String username, char[] password,InitialContext context) {
this.user = username;
this.pass = password;
this.plogin = new ProgrammaticLogin();
//add more code here if needed
}
public void run() {
//insert code here when thread will run
}
}
and invoke it thus:
Runnable thread1 = new MyThread("my user1","my pass1",ContextObject1);
Runnable thread2 = new MyThread("my user2","my pass2",ContextObject2);
new Thread(thread1).start();
new Thread(thread2).start();
Of course this is a very simple example and it might not be suitable for your exact needs, but i think it is a good start for what you need. Since each Context and login credentials will run on a different thread they will have their own separate execution stack and you should not experience any concurrency issues (two threads accessing the same object).
However, you should have a good understanding of concurrency and threads otherwise you might run into different exceptions, that are a bit harder to debug due to using multiple threads.
Tom.
I am writing a java ee application using jsf. I defined some backround processes such as upating the database periodically etc. Here is the code:
public class AppServletContextListener implements ServletContextListener{
#Override
public void contextInitialized(ServletContextEvent arg0) {
zamanli zm = new zamanli();
try {
zm.programBasla();
} catch (MalformedURLException ex) {
Logger.getLogger(AppServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AppServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And the class:
public class zamanli {
public void programBasla() throws MalformedURLException, IOException {
int delay = 5000; //5 sn sonra başlar
int period = 600000; //10 dkda tekrar
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
// p = r.exec("c:\\WINDOWS\\system32\\calc");
System.out.println(Now());
} catch (Exception e) {
System.out.println("Çalışmadı");
}
try {
getCurrentExchangeValue();
} catch (MalformedURLException ex) {
Logger.getLogger(zamanli.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(zamanli.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
The problem is, after program finishes, and even though i close project, and so my database keeps being updated. So how can i kill the thread when program closes?
Thanks
Use a ScheduledExecutorService instead of a Timer, and use a ThreadFactory which spawns a daemon thread instead of a normal thread:
private static final ThreadFactory THREAD_FACTORY = new ThreadFactory()
{
private final ThreadFactory factory = Executors.defaultThreadFactory();
#Override
public Thread newThread(final Runnable r)
{
final Thread ret = factory.newThread(r);
ret.setDaemon(true);
return ret;
}
};
// ...
private final ScheduledExecutorService service
= Executors.newSingleThreadScheduledExecutor(THREAD_FACTORY);
//...
service.scheduleAtFixedRate(etc etc);
Make it so that the service reference is available to contextDestroyed(), that will be even easier; you then don't have to use daemon threads and can just call service.shutdownNow() in it.
As far as I can see, you should add a method to your AppServletContextListener called contextDestroyed(ServletContextEvent). Store your zamanli object as instance variable for the AppServletContextListener class and use the contextDestroyed method to stop the zamanli.
But in general I would recommend not to start your own threads in a Java EE environment.
How can I send and receive from the same program in java ? To make matters worse, I need to do both in the same time in parallel.
You need a well behaved queue such as a BlockingQueue between two Threads.
public class TwoThreads {
static final String FINISHED = "Finished";
public static void main(String[] args) throws InterruptedException {
// The queue
final BlockingQueue<String> q = new ArrayBlockingQueue<String>(10);
// The sending thread.
new Thread() {
#Override
public void run() {
String message = "Now is the time for all good men to come to he aid of the party.";
try {
// Send each word.
for (String word : message.split(" ")) {
q.put(word);
}
// Then the terminator.
q.put(FINISHED);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
{ start();}
};
// The receiving thread.
new Thread() {
#Override
public void run() {
try {
String word;
// Read each word until finished is detected.
while ((word = q.take()) != FINISHED) {
System.out.println(word);
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
{ start();}
};
}
}