Search in sources :

Example 1 with Metric

use of javax.batch.runtime.Metric in project wildfly by wildfly.

the class StepExecutionMarshaller method unmarshall.

public static StepExecution unmarshall(final String json) throws IOException, ClassNotFoundException {
    final JsonParser parser = Json.createParser(new StringReader(json));
    final StepExecutionBuilder builder = StepExecutionBuilder.create();
    String key = null;
    while (parser.hasNext()) {
        final Event event = parser.next();
        switch(event) {
            case KEY_NAME:
                key = parser.getString();
                break;
            case VALUE_FALSE:
            case VALUE_NULL:
            case VALUE_NUMBER:
            case VALUE_STRING:
            case VALUE_TRUE:
                final String value = parser.getString();
                if (key == null) {
                    throw new IllegalStateException(String.format("No key for value '%s'. Parsing position: %s%n\t%s", value, parser.getLocation(), json));
                }
                switch(key) {
                    case ID:
                        if (value != null) {
                            builder.setId(Long.parseLong(value));
                        }
                        break;
                    case NAME:
                        builder.setName(value);
                        break;
                    case STATUS:
                        if (value != null) {
                            builder.setStatus(BatchStatus.valueOf(value));
                        }
                        break;
                    case EXIT_STATUS:
                        builder.setExitStatus(value);
                        break;
                    case END_TIME:
                        if (value != null) {
                            builder.setEndTime(Long.parseLong(value));
                        }
                        break;
                    case START_TIME:
                        if (value != null) {
                            builder.setStartTime(Long.parseLong(value));
                        }
                        break;
                    case PERSISTENT_USER_DATA:
                        builder.setPersistentUserData(deserialize(value));
                    case METRICS:
                        String k = null;
                        String metricType = null;
                        String metricValue = null;
                        while (parser.hasNext()) {
                            final Event e = parser.next();
                            switch(e) {
                                case KEY_NAME:
                                    k = parser.getString();
                                    break;
                                case VALUE_FALSE:
                                case VALUE_NULL:
                                case VALUE_NUMBER:
                                case VALUE_STRING:
                                case VALUE_TRUE:
                                    if (k == null) {
                                        throw new IllegalStateException(String.format("No key for value '%s'. Parsing position: %s%n\t%s", value, parser.getLocation(), json));
                                    }
                                    switch(k) {
                                        case METRIC_TYPE:
                                            metricType = parser.getString();
                                            break;
                                        case METRIC_VALUE:
                                            metricValue = parser.getString();
                                            break;
                                    }
                                    if (metricType != null && metricValue != null) {
                                        final MetricType type = MetricType.valueOf(metricType);
                                        final long v = Long.parseLong(parser.getString());
                                        final Metric m = new Metric() {

                                            @Override
                                            public MetricType getType() {
                                                return type;
                                            }

                                            @Override
                                            public long getValue() {
                                                return v;
                                            }
                                        };
                                        builder.addMetric(m);
                                    }
                                    break;
                            }
                        }
                        break;
                }
                break;
        }
    }
    parser.close();
    return builder.build();
}
Also used : MetricType(javax.batch.runtime.Metric.MetricType) StringReader(java.io.StringReader) Event(javax.json.stream.JsonParser.Event) Metric(javax.batch.runtime.Metric) JsonParser(javax.json.stream.JsonParser)

Example 2 with Metric

use of javax.batch.runtime.Metric in project wildfly by wildfly.

the class StepExecutionMarshaller method marshall.

public static String marshall(final StepExecution stepExecution) throws IOException {
    final StringWriter writer = new StringWriter();
    final JsonGenerator generator = Json.createGenerator(writer);
    generator.writeStartObject().write(ID, stepExecution.getStepExecutionId()).write(NAME, stepExecution.getStepName()).write(STATUS, stepExecution.getBatchStatus().toString()).write(START_TIME, stepExecution.getStartTime().getTime()).write(END_TIME, stepExecution.getEndTime().getTime()).write(EXIT_STATUS, stepExecution.getExitStatus()).write(PERSISTENT_USER_DATA, serialize(stepExecution.getPersistentUserData()));
    generator.writeStartObject(METRICS);
    for (Metric metric : stepExecution.getMetrics()) {
        generator.writeStartObject(METRIC);
        generator.write(METRIC_TYPE, metric.getType().toString());
        generator.write(METRIC_VALUE, metric.getValue());
        generator.writeEnd();
    }
    generator.writeEnd();
    // End main object
    generator.writeEnd();
    generator.close();
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(javax.json.stream.JsonGenerator) Metric(javax.batch.runtime.Metric)

Aggregations

Metric (javax.batch.runtime.Metric)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 MetricType (javax.batch.runtime.Metric.MetricType)1 JsonGenerator (javax.json.stream.JsonGenerator)1 JsonParser (javax.json.stream.JsonParser)1 Event (javax.json.stream.JsonParser.Event)1