Announcement Announcement Module
Collapse
No announcement yet.
Service or new Thread? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Service or new Thread?

    I have this 'socket' client which is connected to server all the time - reading data from input stream. Since I never get -1 from server (full time socket connection), I must ran this code in a new Thread.

    Is there any better way to do this? Run it as some sort of service? Do the core bean factories provide some nice solution to wire this up from context?

    Regards, Ales

    Code:
    public class ClientThread implements Runnable {
        private InputStream in = null;
    
        public ClientThread(Socket socket) {
            try {
                in = socket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void run() {
            int fromServer = 0;
            try {
                while ((fromServer = in.read()) != -1) {
                //do something
                }
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  • #2
    Currently there's no support for something like 'services' in Spring AFAIK. You could however implement a BeanFactoryPostProcessor that inspects the context for all 'runnables', creates and starts a new thread for each of them.

    Comment

    Working...
    X