2

I am executing following ActiveMq program, to put the files in queues.

public static void main(String[] args) throws JMSException, IOException {
    FileInputStream in;
    //Write the file from some location that is to be uploaded on ActiveMQ server

    in = new FileInputStream("d:\\test-transfer-doc-1.docx");
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                    "tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://admin:admin@localhost:8161/fileserver/");
    ActiveMQConnection connection = (ActiveMQConnection) connectionFactory
                    .createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue destination = session.createQueue("stream.file");
    OutputStream out = connection.createOutputStream(destination);

    // now write the file on to ActiveMQ
    byte[] buffer = new byte[1024];
    for (int bytesRead=0;bytesRead!=-1;) {
            bytesRead = in.read(buffer);
            System.out.println("bytes read="+bytesRead);
            if (bytesRead == -1) {
                out.close();
                System.out.println("Now existiong from prog");
                //System.exit(0);
                break;
            }
            else{
                System.out.println("sender\r\n");
                out.write(buffer, 0, bytesRead);
            }
    }
    System.out.println("After for loop");  
}

When the bytesRead == -1, I want to unload the program from JVM as its task is completed. For this purpose, I had initially, used break keyword, and its producing following output on Eclipse Console

enter image description here

using this, the program doesn't get unload form JVM, as the console RED button is still active.

Now I used, System.exit(0) for this purpose, and its exiting the program from the JVM. But I don't want to use System.exit(0) for this purpose. I tried to use simple void return too in place of break but its also not working.

Please tell why break and return aren't working in this case to end the program and why its still running?

Regards,

Arun

Arun Kumar
  • 6,534
  • 13
  • 40
  • 67

4 Answers4

5

You need to close your resources (queues, inputstreams) since they can keep the program alive, after that a simple return will suffice to end the program.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • can return be blocked by the resources? If the code has reached the return statement, then it should simply return, isnt? – Juned Ahsan Jul 10 '13 at 05:41
  • 1
    If there are non-daemon threads running, only the main thread will finish. The process will still continue running until all threads have finished. – Kayaman Jul 10 '13 at 05:46
2

Both return and break work. The problem is that connection.start(); starts a Thread (you can see it in debug mode under the name of [Active MQ Transport...]) and until there exists a running non-daemon thread JVM does not terminate unless System.exit is called.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2

The reason for the JVM still lingering could be some user thread is still hasn't finished. From a preliminary investigation I believe your Session internally runs a thread. I believe you should be stoppping your session and closing your connection before breaking. That should probably resolve your issue.

Drona
  • 6,886
  • 1
  • 29
  • 35
1

The JVM runs as long as a non demon thread is running - so all the people above are correct: You have to close all the resources so the non-demon threads shutdown.

morpheus05
  • 4,772
  • 2
  • 32
  • 47