voodoodyne / subethasmtp

SubEtha SMTP is a Java library for receiving SMTP mail
Other
343 stars 138 forks source link

Restart of the SMTPServer by calling stop-start #75

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Creat Wiser instance and start the server by calling wiser.start().
2. Call wiser.stop() to stop the SMTPServer.
3. Call wiser.start() on the same instance to start the SMTPServer again.

What is the expected output? What do you see instead?
The SMTPServer should be started again, but the second call of the 
wiser.start() throws the "java.lang.IllegalStateException: SMTPServer can only 
be started once", because is started flag is not set to false in the 
stop-method.

What version of the product are you using? On what operating system?
3.1.7

Original issue reported on code.google.com by beast.sh...@gmail.com on 3 Feb 2015 at 12:28

amarhin commented 8 years ago

When you start the server with wiser.start(), an inner flag 'started' is set to true in the SMTPServer class; this one is not set to false when you stop the server with wiser.stop().

// True if this SMTPServer was started. It remains true even if the SMTPServer has been stopped since @GuardedBy("this") private boolean started = false;

It seems to be a deliberate comportment.

You have to instanciate a new server each time you want to create a new one.

wiser.start() // create a new server wiser.stop() // stop the server and release resources (close sockets...) wiser = new Wiser(port); // create a new one

Hope these few explanations will help you.

arichiardi commented 7 years ago

Hello! I read on this line: https://github.com/voodoodyne/subethasmtp/blob/master/src/main/java/org/subethamail/smtp/server/SMTPServer.java#L72

that the thread pool cannot be restarted...isn't ExecutorServices.shutdownNow an option?

Would you accept a PR to make the server fully stoppable/reloadable?

arichiardi commented 7 years ago

This is what I do in Clojure, using reflection, it is super hacky but it works so I wonder whether a PR would be effective here.

  ;; AR - workaround - don't do this at home
  (let [{:keys [server]} state
        field (doto (.. server getClass (getDeclaredField "executorService")) (.setAccessible true))
        executor-svc (. field (get server))]
    (.stop server)
    (.shutdownNow executor-svc))