source: java/main/src/main/java/com/framsticks/diagnostics/DiagnosticsEndpoint.java @ 77

Last change on this file since 77 was 77, checked in by psniegowski, 11 years ago

Add new java codebase.

File size: 2.9 KB
Line 
1package com.framsticks.diagnostics;
2
3import com.framsticks.core.Path;
4import com.framsticks.dumping.SaveStream;
5import com.framsticks.observers.Endpoint;
6import com.framsticks.dumping.PrintStreamSink;
7import com.framsticks.remote.RecursiveFetcher;
8import com.framsticks.util.Logging;
9import com.framsticks.util.PeriodicTask;
10import com.framsticks.util.StateFunctor;
11import org.apache.log4j.Logger;
12
13import java.io.File;
14import java.io.IOException;
15import java.io.PrintStream;
16import java.text.SimpleDateFormat;
17import java.util.Date;
18
19/**
20 * @author Piotr Sniegowski
21 */
22public class DiagnosticsEndpoint extends Endpoint {
23
24    private final static Logger LOGGER = Logger.getLogger(DiagnosticsEndpoint.class.getName());
25
26
27    public DiagnosticsEndpoint() {
28        super();
29    }
30
31    @Override
32    public Diagnostics getObserver() {
33        return (Diagnostics)observer;
34    }
35
36
37    @Override
38    protected void configure() {
39        super.configure();
40
41        if (getObserver().dumpsInterval != null) {
42            new PeriodicTask(instance, getObserver().dumpsInterval * 1000) {
43                @Override
44                public void run() {
45
46                    LOGGER.info("starting periodic dump");
47                    new RecursiveFetcher(instance, new Path(instance, "/"), new StateFunctor() {
48                        @Override
49                        public void call(Exception e) {
50                            if (Logging.log(LOGGER, "recursively fetch", instance, e)) {
51                                again();
52                                return;
53                            }
54                            LOGGER.info("instance resolved, saving");
55                            try {
56                                final String fileName = getObserver().dumpsPath + "/" + instance + "_" + new SimpleDateFormat(getObserver().dumpsFormat).format(new Date()) + ".param";
57                                File file = new File(fileName);
58                                new SaveStream(new PrintStreamSink(new PrintStream(file)), instance, new Path(instance, "/"), new StateFunctor() {
59                                    @Override
60                                    public void call(Exception e) {
61                                        Logging.log(LOGGER, "periodic dump in " + fileName + " of", DiagnosticsEndpoint.this.instance, e);
62                                        again();
63                                    }
64                                });
65                            } catch (IOException ex) {
66                                LOGGER.info("failed to initiate dump: " + ex);
67                                again();
68                            }
69
70                        }
71                    });
72
73
74                }
75            };
76        }
77    }
78
79    /*
80    @Override
81    public void onChange(Path path) {
82        super.onChange(path);    //To change body of overridden methods use File | Settings | File Templates.
83    }
84    */
85}
Note: See TracBrowser for help on using the repository browser.