@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { EntityManager entityManager = entityManagerProvider.getEntityManager(); if (fetchSegments(processorName).length > 0) { throw new UnableToClaimTokenException("Could not initialize segments. Some segments were already present."); } for (int segment = 0; segment < segmentCount; segment++) { TokenEntry token = new TokenEntry(processorName, segment, initialToken, serializer); entityManager.persist(token); } entityManager.flush(); }
/** * Loads an existing {@link TokenEntry} or creates a new one using the given {@code entityManager} for given {@code * processorName} and {@code segment}. * * @param processorName the name of the event processor * @param segment the segment of the event processor * @param entityManager the entity manager instance to use for the query * @return the token entry for the given processor name and segment * @throws UnableToClaimTokenException if there is a token for given {@code processorName} and {@code segment}, but * it is claimed by another process. */ protected TokenEntry loadOrCreateToken(String processorName, int segment, EntityManager entityManager) { TokenEntry token = entityManager .find(TokenEntry.class, new TokenEntry.PK(processorName, segment), loadingLockMode); if (token == null) { token = new TokenEntry(processorName, segment, null, serializer); token.claim(nodeId, claimTimeout); entityManager.persist(token); // hibernate complains about updates in different transactions if this isn't flushed entityManager.flush(); } else if (!token.claim(nodeId, claimTimeout)) { throw new UnableToClaimTokenException( format("Unable to claim token '%s[%s]'. It is owned by '%s'", token.getProcessorName(), token.getSegment(), token.getOwner())); } return token; }
@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { EntityManager entityManager = entityManagerProvider.getEntityManager(); if (fetchSegments(processorName).length > 0) { throw new UnableToClaimTokenException("Could not initialize segments. Some segments were already present."); } for (int segment = 0; segment < segmentCount; segment++) { TokenEntry token = new TokenEntry(processorName, segment, initialToken, serializer); entityManager.persist(token); } entityManager.flush(); }
@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { EntityManager entityManager = entityManagerProvider.getEntityManager(); if (fetchSegments(processorName).length > 0) { throw new UnableToClaimTokenException("Could not initialize segments. Some segments were already present."); } for (int segment = 0; segment < segmentCount; segment++) { TokenEntry token = new TokenEntry(processorName, segment, initialToken, serializer); entityManager.persist(token); } entityManager.flush(); }
/** * Loads an existing {@link TokenEntry} or creates a new one using the given {@code entityManager} for given {@code * processorName} and {@code segment}. * * @param processorName the name of the event processor * @param segment the segment of the event processor * @param entityManager the entity manager instance to use for the query * @return the token entry for the given processor name and segment * @throws UnableToClaimTokenException if there is a token for given {@code processorName} and {@code segment}, but * it is claimed by another process. */ protected TokenEntry loadOrCreateToken(String processorName, int segment, EntityManager entityManager) { TokenEntry token = entityManager .find(TokenEntry.class, new TokenEntry.PK(processorName, segment), LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap("javax.persistence.query.timeout", 1)); if (token == null) { token = new TokenEntry(processorName, segment, null, serializer); token.claim(nodeId, claimTimeout); entityManager.persist(token); // hibernate complains about updates in different transactions if this isn't flushed entityManager.flush(); } else if (!token.claim(nodeId, claimTimeout)) { throw new UnableToClaimTokenException( format("Unable to claim token '%s[%s]'. It is owned by '%s'", token.getProcessorName(), token.getSegment(), token.getOwner())); } return token; }
/** * Loads an existing {@link TokenEntry} or creates a new one using the given {@code entityManager} for given {@code * processorName} and {@code segment}. * * @param processorName the name of the event processor * @param segment the segment of the event processor * @param entityManager the entity manager instance to use for the query * @return the token entry for the given processor name and segment * * @throws UnableToClaimTokenException if there is a token for given {@code processorName} and {@code segment}, but * it is claimed by another process. */ protected TokenEntry loadOrCreateToken(String processorName, int segment, EntityManager entityManager) { TokenEntry token = entityManager .find(TokenEntry.class, new TokenEntry.PK(processorName, segment), LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap("javax.persistence.query.timeout", 1)); if (token == null) { token = new TokenEntry(processorName, segment, null, serializer); token.claim(nodeId, claimTimeout); entityManager.persist(token); // hibernate complains about updates in different transactions if this isn't flushed entityManager.flush(); } else if (!token.claim(nodeId, claimTimeout)) { throw new UnableToClaimTokenException( format("Unable to claim token '%s[%s]'. It is owned by '%s'", token.getProcessorName(), token.getSegment(), token.getOwner())); } return token; }