Question

I'd like to create a Receiver to gather and manage logs send by JMSAppender. Now it works only 30 seconds (Thread.sleep(30000);), but I need it to be waiting for all logs from system. How can I do this if I need to close pw.close(); to see all logs in file? From file I load my logs to log viewer and I'd like to see them in real time.

public class Receiver implements MessageListener {

    PrintWriter pw = new PrintWriter("result.log");

    public Receiver() throws Exception {


        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection conn = factory.createConnection();
        Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        conn.start();
        MessageConsumer consumer = sess.createConsumer(sess.createTopic("logTopic"));
        consumer.setMessageListener(this);

        Thread.sleep(30000);
        consumer.close();
        sess.close();
        conn.close();
        pw.close();
        System.exit(1);
    }

    public static void main(String[] args) throws Exception {
        new Receiver();

    }

    public void onMessage(Message message) {
        try {
            LoggingEvent event = (LoggingEvent) ((ActiveMQObjectMessage) message).getObject();

            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS"); 
            String nowAsString = df.format(new Date(event.getTimeStamp())); 

            System.out.println("zapisujemy do pliku");
            pw.println("["+ nowAsString + "]" + 
                    " [" + event.getThreadName()+"]" +
                    " ["+ event.getLoggerName() + "]" +
                    " ["+ event.getMessage()+"]");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Était-ce utile?

La solution 4

Finally I've written receiver:

public class Receiver implements MessageListener {

    static Connection conn;
    ActiveMQConnectionFactory factory;
    static Session sess;
    static MessageConsumer consumer;


    public Receiver() throws Exception {

        /*
         *  create a logTopic topic consumer
         */
        factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        conn = factory.createConnection();
        sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        conn.start();
        consumer = sess.createConsumer(sess.createTopic("logTopic"));
        consumer.setMessageListener(this);

    }

    public static void main(String[] args) throws Exception {

        Runtime.getRuntime().addShutdownHook(new Thread(new ShutDownListener(consumer, sess, conn)));
        new Receiver();
    }


    public void onMessage(Message message) {
        try {
            LoggingEvent event = (LoggingEvent) ((ActiveMQObjectMessage) message)
                    .getObject();

            /*
             * log on console and into a file
             */

            FileReceiver.logToFile(event);
            StdoutReceiver.logOnConsole(event);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

With FileReceiver:

public class FileReceiver {

    static PrintWriter pw;
    static DateFormat df;

    static {
        try {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
            pw = new PrintWriter("result.log");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                pw.close();
                System.exit(1);
            }
        });
    }

    public static void logToFile(LoggingEvent event) {
        String nowAsString = df.format(new Date(event.getTimeStamp()));

        pw.println("[" + nowAsString + "]" + " [" + event.getThreadName() + "]"
                + " [" + event.getLoggerName() + "]" + " ["
                + event.getMessage() + "]");
        pw.flush();


    }
}

and StdOutReceiver

public class StdoutReceiver {

    public static void logOnConsole(LoggingEvent event) {

        System.out.println("[" + event.getTimeStamp() + "]" + " ["
                + event.getThreadName() + "]" + " [" + event.getLoggerName()
                + "]" + " [" + event.getMessage() + "]");
    }

}

Autres conseils

Do not close the print writer in constructor and remove your Thread.sleep(). Your program will not terminate because you are calling conn.start(); that creates waiting thread into the JMS client.

Add pw.flush(); after pw.println() into onMessage()

EDIT: you do not have to close file. But if you want to create clean implementation do it in shutdown hook.

I feel like I'm pointing out the obvious here, but you do realise that you are waiting for 30 seconds in your constructor?

If you want to close the connection after 30 seconds, then I would recommend using Timer and doing a threadsafe close of the connection there.

A consequence of pausing in your constructor is that when other threads call onMessage, your object hasn't finished being created yet and could behave erratically.

That is because you close it after those 30 secondes, and then even exit the jvm.

Do the closing on JVM exit: Runtime.addShutdownHook

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top