Search in sources :

Example 21 with ServiceUnavailableException

use of org.zalando.nakadi.exceptions.ServiceUnavailableException in project nakadi by zalando.

the class SubscriptionDbRepository method listSubscriptions.

public List<Subscription> listSubscriptions(final Set<String> eventTypes, final Optional<String> owningApplication, final int offset, final int limit) throws ServiceUnavailableException {
    final StringBuilder queryBuilder = new StringBuilder("SELECT s_subscription_object FROM zn_data.subscription ");
    final List<String> clauses = Lists.newArrayList();
    final List<Object> params = Lists.newArrayList();
    owningApplication.ifPresent(owningApp -> {
        clauses.add(" s_subscription_object->>'owning_application' = ? ");
        params.add(owningApp);
    });
    if (!eventTypes.isEmpty()) {
        final String clause = eventTypes.stream().map(et -> " s_subscription_object->'event_types' @> ?::jsonb").collect(Collectors.joining(" AND "));
        clauses.add(clause);
        eventTypes.stream().map(et -> format("\"{0}\"", et)).forEach(params::add);
    }
    if (!clauses.isEmpty()) {
        queryBuilder.append(" WHERE ");
        queryBuilder.append(StringUtils.join(clauses, " AND "));
    }
    queryBuilder.append(" ORDER BY s_subscription_object->>'created_at' DESC LIMIT ? OFFSET ? ");
    params.add(limit);
    params.add(offset);
    try {
        return jdbcTemplate.query(queryBuilder.toString(), params.toArray(), rowMapper);
    } catch (final DataAccessException e) {
        LOG.error("Database error when listing subscriptions", e);
        throw new ServiceUnavailableException("Error occurred when running database request");
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) DataAccessException(org.springframework.dao.DataAccessException) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) NoSubscriptionException(org.zalando.nakadi.exceptions.runtime.NoSubscriptionException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Subscription(org.zalando.nakadi.domain.Subscription) StringUtils(org.apache.commons.lang3.StringUtils) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) UUIDGenerator(org.zalando.nakadi.util.UUIDGenerator) SQLException(java.sql.SQLException) MessageFormat.format(java.text.MessageFormat.format) Lists(com.google.common.collect.Lists) Sets.newTreeSet(com.google.common.collect.Sets.newTreeSet) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) ResultSet(java.sql.ResultSet) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Profile(org.springframework.context.annotation.Profile) Collectors(java.util.stream.Collectors) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) Component(org.springframework.stereotype.Component) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) RowMapper(org.springframework.jdbc.core.RowMapper) NoSuchSubscriptionException(org.zalando.nakadi.exceptions.NoSuchSubscriptionException) DuplicatedSubscriptionException(org.zalando.nakadi.exceptions.runtime.DuplicatedSubscriptionException) HashGenerator(org.zalando.nakadi.util.HashGenerator) Optional(java.util.Optional) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) DataAccessException(org.springframework.dao.DataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Aggregations

ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)21 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 TopicRepository (org.zalando.nakadi.repository.TopicRepository)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Optional (java.util.Optional)7 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)7 Timeline (org.zalando.nakadi.domain.Timeline)7 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)7 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)7 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)7 HashMap (java.util.HashMap)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 UUIDGenerator (org.zalando.nakadi.util.UUIDGenerator)6 Collections (java.util.Collections)5 Set (java.util.Set)5 Stream (java.util.stream.Stream)5