Java Code Examples for com.fasterxml.jackson.core.JsonProcessingException#toString()

The following examples show how to use com.fasterxml.jackson.core.JsonProcessingException#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DaprBeanPostProcessor.java    From java-sdk with MIT License 5 votes vote down vote up
/**
 * Subscribe to topics based on {@link Topic} annotations on the given class and any of ancestor classes.
 * @param clazz Controller class where {@link Topic} is expected.
 */
private static void subscribeToTopics(Class clazz) {
  if (clazz == null) {
    return;
  }

  subscribeToTopics(clazz.getSuperclass());
  for (Method method : clazz.getDeclaredMethods()) {
    Topic topic = method.getAnnotation(Topic.class);
    if (topic == null) {
      continue;
    }

    String route = topic.name();
    PostMapping mapping = method.getAnnotation(PostMapping.class);

    if (mapping != null && mapping.path() != null && mapping.path().length >= 1) {
      route = mapping.path()[0];
    }

    String topicName = topic.name();
    if ((topicName != null) && (topicName.length() > 0)) {
      try {
        TypeReference<HashMap<String, String>> typeRef
                = new TypeReference<HashMap<String, String>>() {};
        Map<String, String> metadata = MAPPER.readValue(topic.metadata(), typeRef);
        DaprRuntime.getInstance().addSubscribedTopic(topicName, route, metadata);
      } catch (JsonProcessingException e) {
        throw new IllegalArgumentException("Error while parsing metadata: " + e.toString());
      }
    }
  }
}
 
Example 2
Source File: MonitorServer.java    From demo-java-9-migration with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
private static String toJson(Statistics stats) {
	try {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(StatisticsEntity.from(stats));
	} catch (JsonProcessingException ex) {
		// don't do this in real live
		return ex.toString();
	}
}
 
Example 3
Source File: JacksonConfigParser.java    From java-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public String toJson(Object src) throws JsonParseException {
    try {
        return objectMapper.writeValueAsString(src);
    } catch (JsonProcessingException e) {
        throw new JsonParseException("Serialization failed: " + e.toString());
    }
}
 
Example 4
Source File: MonitorServer.java    From jdk9-jigsaw with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
private static String toJson(Statistics stats) {
	try {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(StatisticsEntity.from(stats));
	} catch (JsonProcessingException ex) {
		// don't do this in real live
		return ex.toString();
	}
}
 
Example 5
Source File: MonitorServer.java    From jdk9-jigsaw with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
private static String toJson(Statistics stats) {
	try {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(StatisticsEntity.from(stats));
	} catch (JsonProcessingException ex) {
		// don't do this in real live
		return ex.toString();
	}
}
 
Example 6
Source File: StatementUpdate.java    From Wikidata-Toolkit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a JSON serialization of the marked insertions and deletions of
 * statements, in the format required by the Wikibase "wbeditentity" action.
 *
 * @return JSON serialization of updates
 */
@JsonIgnore
public String getJsonUpdateString() {
	try {
		return mapper.writeValueAsString(this);
	} catch (JsonProcessingException e) {
		return ("Failed to serialize statement update to JSON: " + e.toString());
	}
}
 
Example 7
Source File: JSONBase.java    From TinCanJava with Apache License 2.0 5 votes vote down vote up
@Override
public String toJSON(TCAPIVersion version, Boolean pretty) {
    ObjectWriter writer = Mapper.getWriter(pretty);
    try {
        return writer.writeValueAsString(this.toJSONNode(version));
    } catch (JsonProcessingException ex) {
        return "Exception in JSONBase Class: " + ex.toString();
    }
}
 
Example 8
Source File: StandaloneRunner.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private void validateAndSetStateTransition(String user, PipelineStatus toStatus, String message, Map<String, Object> attributes)
  throws PipelineStoreException, PipelineRunnerException {
  PipelineState fromState;
  PipelineState pipelineState;
  synchronized (this) {
    fromState = getState();
    checkState(VALID_TRANSITIONS.get(fromState.getStatus()).contains(toStatus), ContainerError.CONTAINER_0102,
      fromState.getStatus(), toStatus);
    long nextRetryTimeStamp = fromState.getNextRetryTimeStamp();
    int retryAttempt = fromState.getRetryAttempt();
    String metricString = null;
    if (toStatus == PipelineStatus.RETRY && fromState.getStatus() != PipelineStatus.CONNECTING) {
      retryAttempt = fromState.getRetryAttempt() + 1;
      if (retryAttempt > maxRetries && maxRetries != -1) {
        LOG.info("Retry attempt '{}' is greater than max no of retries '{}'", retryAttempt, maxRetries);
        toStatus = PipelineStatus.RUN_ERROR;
        retryAttempt = 0;
        nextRetryTimeStamp = 0;
      } else {
        nextRetryTimeStamp = RetryUtils.getNextRetryTimeStamp(retryAttempt, System.currentTimeMillis());
        isRetrying = true;
        metricsForRetry = getState().getMetrics();
      }
    } else if (!toStatus.isActive()) {
      retryAttempt = 0;
      nextRetryTimeStamp = 0;
    }
    if (!toStatus.isActive() || toStatus == PipelineStatus.DISCONNECTED
      || (toStatus == PipelineStatus.RETRY && fromState.getStatus() != PipelineStatus.CONNECTING)) {
      Object metrics = getMetrics();
      if (metrics != null) {
        ObjectMapper objectMapper = ObjectMapperFactory.get();
        try {
          metricString = objectMapper.writeValueAsString(metrics);
        } catch (JsonProcessingException e) {
          throw new PipelineStoreException(ContainerError.CONTAINER_0210, e.toString(), e);
        }
        getEventListenerManager().broadcastMetrics(getName(), metricString);
      }
      if (metricString == null) {
        metricString = getState().getMetrics();
      }
    }
    pipelineState =
      getPipelineStateStore().saveState(user, getName(), getRev(), toStatus, message, attributes, ExecutionMode.STANDALONE,
        metricString, retryAttempt, nextRetryTimeStamp);
    if (toStatus == PipelineStatus.RETRY) {
      retryFuture = scheduleForRetries(runnerExecutor);
    }
  }
  statsCollector.pipelineStatusChanged(toStatus, getPipelineConfigurationIfExists(), getPipeline());
  getEventListenerManager().broadcastStateChange(
      fromState,
      pipelineState,
      ThreadUsage.STANDALONE,
      OffsetFileUtil.getOffsets(getRuntimeInfo(), getName(), getRev())
  );
}
 
Example 9
Source File: ClusterRunner.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
void validateAndSetStateTransition(String user, PipelineStatus toStatus, String message, Map<String, Object> attributes)
  throws PipelineStoreException, PipelineRunnerException {
  Utils.checkState(attributes!=null, "Attributes cannot be set to null");
  PipelineState fromState = getState();
  if (fromState.getStatus() == toStatus && toStatus != PipelineStatus.STARTING) {
    LOG.debug(Utils.format("Ignoring status '{}' as this is same as current status", fromState.getStatus()));
  } else {
    PipelineState pipelineState;
    synchronized (this) {
      fromState = getState();
      checkState(VALID_TRANSITIONS.get(fromState.getStatus()).contains(toStatus), ContainerError.CONTAINER_0102,
        fromState.getStatus(), toStatus);
      long nextRetryTimeStamp = fromState.getNextRetryTimeStamp();
      int retryAttempt = fromState.getRetryAttempt();
      if (toStatus == PipelineStatus.RUN_ERROR) {
        handleErrorCallbackFromSlaves(attributes);
      }
      if (toStatus == PipelineStatus.RUN_ERROR && shouldRetry) {
        toStatus = PipelineStatus.RETRY;
        checkState(VALID_TRANSITIONS.get(fromState.getStatus()).contains(toStatus), ContainerError.CONTAINER_0102,
          fromState.getStatus(), toStatus);
      }
      if (toStatus == PipelineStatus.RETRY && fromState.getStatus() != PipelineStatus.CONNECTING) {
        retryAttempt = fromState.getRetryAttempt() + 1;
        if (retryAttempt > maxRetries && maxRetries != -1) {
          LOG.info("Retry attempt '{}' is greater than max no of retries '{}'", retryAttempt, maxRetries);
          toStatus = PipelineStatus.RUN_ERROR;
          retryAttempt = 0;
          nextRetryTimeStamp = 0;
        } else {
          nextRetryTimeStamp = RetryUtils.getNextRetryTimeStamp(retryAttempt, System.currentTimeMillis());
        }
      } else if (!toStatus.isActive()) {
        retryAttempt = 0;
        nextRetryTimeStamp = 0;
      }
      ObjectMapper objectMapper = ObjectMapperFactory.get();
      String metricsJSONStr = null;
      if (!toStatus.isActive() || toStatus == PipelineStatus.DISCONNECTED) {
        Object metrics = getMetrics();
        if (metrics != null) {
          try {
            metricsJSONStr = objectMapper.writer().writeValueAsString(metrics);
          } catch (JsonProcessingException e) {
            throw new PipelineStoreException(ContainerError.CONTAINER_0210, e.toString(), e);
          }
        }
        if (metricsJSONStr == null) {
          metricsJSONStr = getState().getMetrics();
        }
      }
      pipelineState =
        getPipelineStateStore().saveState(user, getName(), getRev(), toStatus, message, attributes, getState().getExecutionMode(),
          metricsJSONStr, retryAttempt, nextRetryTimeStamp);
      if (toStatus == PipelineStatus.RETRY) {
        retryFuture = scheduleForRetries(runnerExecutor);
      }
    }
    // This should be out of sync block
    statsCollector.pipelineStatusChanged(toStatus, pipelineConf, null);
    if (getEventListenerManager() != null) {
      getEventListenerManager().broadcastStateChange(
          fromState,
          pipelineState,
          ThreadUsage.CLUSTER,
          OffsetFileUtil.getOffsets(getRuntimeInfo(), getName(), getRev())
      );
    }
  }
}