/** * hmily initialization. * * @param hmilyConfig {@linkplain HmilyConfig} */ @Override public void initialization(final HmilyConfig hmilyConfig) { Runtime.getRuntime().addShutdownHook(new Thread(() -> LOGGER.info("hmily shutdown now"))); try { loadSpiSupport(hmilyConfig); hmilyCoordinatorService.start(hmilyConfig); } catch (Exception ex) { LogUtil.error(LOGGER, " hmily init exception:{}", ex::getMessage); System.exit(1); } new HmilyLogo().logo(); }
@Override public Boolean login(final String userName, final String password) { LogUtil.info(LOGGER, "输入的用户名密码为:{}", () -> userName + "," + password); if (userName.equals(this.userName) && password.equals(this.password)) { LOGIN_SUCCESS = true; return Boolean.TRUE; } return Boolean.FALSE; }
@Override public Object interceptor(final ProceedingJoinPoint pjp) throws Throwable { HmilyTransactionContext hmilyTransactionContext; RequestAttributes requestAttributes = null; try { requestAttributes = RequestContextHolder.currentRequestAttributes(); } catch (Throwable ex) { LogUtil.warn(LOGGER, () -> "can not acquire request info:" + ex.getLocalizedMessage()); } HttpServletRequest request = requestAttributes == null ? null : ((ServletRequestAttributes) requestAttributes).getRequest(); String context = request == null ? null : request.getHeader(CommonConstant.HMILY_TRANSACTION_CONTEXT); if (StringUtils.isNoneBlank(context)) { hmilyTransactionContext = GsonUtils.getInstance().fromJson(context, HmilyTransactionContext.class); } else { hmilyTransactionContext = HmilyTransactionContextLocal.getInstance().get(); if (Objects.nonNull(hmilyTransactionContext)) { hmilyTransactionContext.setRole(HmilyRoleEnum.SPRING_CLOUD.getCode()); } } return hmilyTransactionAspectService.invoke(hmilyTransactionContext, pjp); }
scheduledExecutorService .scheduleWithFixedDelay(() -> { LogUtil.info(LOGGER, "self recovery execute delayTime:{}", () -> hmilyConfig.getScheduledDelay()); try { final List<HmilyTransaction> hmilyTransactions = hmilyCoordinatorRepository.listAllByDelay(acquireData()); LogUtil.error(LOGGER, "This transaction exceeds the maximum number of retries and no retries will occur:{}", () -> hmilyTransaction); continue; LogUtil.error(LOGGER, "execute recover exception:{}", e::getMessage);
LogUtil.debug(LOGGER, () -> "hmily transaction confirm .......!start"); if (Objects.isNull(currentTransaction) || CollectionUtils.isEmpty(currentTransaction.getHmilyParticipants())) { return; executeParticipantMethod(hmilyParticipant.getConfirmHmilyInvocation()); } catch (Exception e) { LogUtil.error(LOGGER, "execute confirm :{}", () -> e); success = false; failList.add(hmilyParticipant);
@SuppressWarnings("unchecked") private void executeCoordinator(final HmilyInvocation hmilyInvocation) throws Exception { if (Objects.nonNull(hmilyInvocation)) { final Class clazz = hmilyInvocation.getTargetClass(); final String method = hmilyInvocation.getMethodName(); final Object[] args = hmilyInvocation.getArgs(); final Class[] parameterTypes = hmilyInvocation.getParameterTypes(); final Object bean = SpringBeanUtils.getInstance().getBean(clazz); MethodUtils.invokeMethod(bean, method, args, parameterTypes); LogUtil.debug(LOGGER, "recovery execute transaction compensation:{}", () -> hmilyInvocation.getTargetClass() + ":" + hmilyInvocation.getMethodName()); } } }
LogUtil.debug(LOGGER, () -> "tcc cancel ...........start!"); if (Objects.isNull(currentTransaction) || CollectionUtils.isEmpty(currentTransaction.getHmilyParticipants())) { return; executeParticipantMethod(hmilyParticipant.getCancelHmilyInvocation()); } catch (Throwable e) { LogUtil.error(LOGGER, "execute cancel ex:{}", () -> e); success = false; failList.add(hmilyParticipant);
/** * transaction preTry. * * @param point cut point. * @return TccTransaction */ public HmilyTransaction preTry(final ProceedingJoinPoint point) { LogUtil.debug(LOGGER, () -> "......hmily transaction starter...."); //build tccTransaction final HmilyTransaction hmilyTransaction = buildHmilyTransaction(point, HmilyRoleEnum.START.getCode(), null); //save tccTransaction in threadLocal CURRENT.set(hmilyTransaction); //publishEvent hmilyTransactionEventPublisher.publishEvent(hmilyTransaction, EventTypeEnum.SAVE.getCode()); //set TccTransactionContext this context transfer remote HmilyTransactionContext context = new HmilyTransactionContext(); //set action is try context.setAction(HmilyActionEnum.TRYING.getCode()); context.setTransId(hmilyTransaction.getTransId()); context.setRole(HmilyRoleEnum.START.getCode()); HmilyTransactionContextLocal.getInstance().set(context); return hmilyTransaction; }
@Override public void init(final String modelName, final HmilyConfig hmilyConfig) { rootPathPrefix = RepositoryPathUtils.buildZookeeperPathPrefix(modelName); try { connect(hmilyConfig.getHmilyZookeeperConfig()); } catch (Exception e) { LogUtil.error(LOGGER, "zookeeper init error please check you config:{}", e::getMessage); throw new HmilyRuntimeException(e.getMessage()); } }
JedisPool jedisPool; if (hmilyRedisConfig.getCluster()) { LogUtil.info(LOGGER, () -> "build redis cluster ............"); final String clusterUrl = hmilyRedisConfig.getClusterUrl(); final Set<HostAndPort> hostAndPorts = jedisClient = new JedisClientCluster(jedisCluster); } else if (hmilyRedisConfig.getSentinel()) { LogUtil.info(LOGGER, () -> "build redis sentinel ............"); final String sentinelUrl = hmilyRedisConfig.getSentinelUrl(); final Set<String> hostAndPorts =
/** * this is Participant transaction preTry. * * @param context transaction context. * @param point cut point * @return TccTransaction */ public HmilyTransaction preTryParticipant(final HmilyTransactionContext context, final ProceedingJoinPoint point) { LogUtil.debug(LOGGER, "participant hmily transaction start..:{}", context::toString); final HmilyTransaction hmilyTransaction = buildHmilyTransaction(point, HmilyRoleEnum.PROVIDER.getCode(), context.getTransId()); //cache by guava HmilyTransactionGuavaCacheManager.getInstance().cacheHmilyTransaction(hmilyTransaction); //publishEvent hmilyTransactionEventPublisher.publishEvent(hmilyTransaction, EventTypeEnum.SAVE.getCode()); //Nested transaction support context.setRole(HmilyRoleEnum.LOCAL.getCode()); HmilyTransactionContextLocal.getInstance().set(context); return hmilyTransaction; }
@Override public void init(final String modelName, final HmilyConfig hmilyConfig) { keyPrefix = RepositoryPathUtils.buildRedisKeyPrefix(modelName); final HmilyRedisConfig hmilyRedisConfig = hmilyConfig.getHmilyRedisConfig(); try { buildJedisPool(hmilyRedisConfig); } catch (Exception e) { LogUtil.error(LOGGER, "redis init error please check you config:{}", e::getMessage); throw new HmilyRuntimeException(e); } }
@Override public void init(final String modelName, final HmilyConfig hmilyConfig) { collectionName = RepositoryPathUtils.buildMongoTableName(modelName); final HmilyMongoConfig hmilyMongoConfig = hmilyConfig.getHmilyMongoConfig(); MongoClientFactoryBean clientFactoryBean = buildMongoClientFactoryBean(hmilyMongoConfig); try { clientFactoryBean.afterPropertiesSet(); template = new MongoTemplate(Objects.requireNonNull(clientFactoryBean.getObject()), hmilyMongoConfig.getMongoDbName()); } catch (Exception e) { LogUtil.error(LOGGER, "mongo init error please check you config:{}", e::getMessage); throw new HmilyRuntimeException(e); } }
/** * Cancel. * * @param hmilyTransaction the hmily transaction */ public void cancel(final HmilyTransaction hmilyTransaction) { final List<HmilyParticipant> hmilyParticipants = hmilyTransaction.getHmilyParticipants(); List<HmilyParticipant> failList = Lists.newArrayListWithCapacity(hmilyParticipants.size()); boolean success = true; if (CollectionUtils.isNotEmpty(hmilyParticipants)) { for (HmilyParticipant hmilyParticipant : hmilyParticipants) { try { HmilyTransactionContext context = new HmilyTransactionContext(); context.setAction(HmilyActionEnum.CANCELING.getCode()); context.setTransId(hmilyTransaction.getTransId()); context.setRole(HmilyRoleEnum.START.getCode()); HmilyTransactionContextLocal.getInstance().set(context); executeCoordinator(hmilyParticipant.getCancelHmilyInvocation()); } catch (Exception e) { LogUtil.error(LOGGER, "execute cancel exception:{}", () -> e); success = false; failList.add(hmilyParticipant); } finally { HmilyTransactionContextLocal.getInstance().remove(); } } executeHandler(success, hmilyTransaction, failList); } }
/** * Confirm. * * @param hmilyTransaction the hmily transaction */ public void confirm(final HmilyTransaction hmilyTransaction) { final List<HmilyParticipant> hmilyParticipants = hmilyTransaction.getHmilyParticipants(); List<HmilyParticipant> failList = Lists.newArrayListWithCapacity(hmilyParticipants.size()); boolean success = true; if (CollectionUtils.isNotEmpty(hmilyParticipants)) { for (HmilyParticipant hmilyParticipant : hmilyParticipants) { try { HmilyTransactionContext context = new HmilyTransactionContext(); context.setAction(HmilyActionEnum.CONFIRMING.getCode()); context.setRole(HmilyRoleEnum.START.getCode()); context.setTransId(hmilyTransaction.getTransId()); HmilyTransactionContextLocal.getInstance().set(context); executeCoordinator(hmilyParticipant.getConfirmHmilyInvocation()); } catch (Exception e) { LogUtil.error(LOGGER, "execute confirm exception:{}", () -> e); success = false; failList.add(hmilyParticipant); } finally { HmilyTransactionContextLocal.getInstance().remove(); } } executeHandler(success, hmilyTransaction, failList); } }
} catch (Exception ex) { ex.printStackTrace(); LogUtil.error(LOGGER, "hmily find method error {} ", ex::getMessage);
executeUpdate(SqlHelper.buildCreateTableSql(hmilyDbConfig.getDriverClassName(), tableName)); } catch (Exception e) { LogUtil.error(LOGGER, "hmily jdbc log init exception please check config:{}", e::getMessage); throw new HmilyRuntimeException(e);
@SuppressWarnings("unchecked") private HmilyTransaction buildByCache(final MongoAdapter cache) { try { HmilyTransaction hmilyTransaction = new HmilyTransaction(); hmilyTransaction.setTransId(cache.getTransId()); hmilyTransaction.setCreateTime(cache.getCreateTime()); hmilyTransaction.setLastTime(cache.getLastTime()); hmilyTransaction.setRetriedCount(cache.getRetriedCount()); hmilyTransaction.setVersion(cache.getVersion()); hmilyTransaction.setStatus(cache.getStatus()); hmilyTransaction.setRole(cache.getRole()); hmilyTransaction.setPattern(cache.getPattern()); hmilyTransaction.setTargetClass(cache.getTargetClass()); hmilyTransaction.setTargetMethod(cache.getTargetMethod()); List<HmilyParticipant> hmilyParticipants = (List<HmilyParticipant>) objectSerializer.deSerialize(cache.getContents(), CopyOnWriteArrayList.class); hmilyTransaction.setHmilyParticipants(hmilyParticipants); return hmilyTransaction; } catch (HmilyException e) { LogUtil.error(LOGGER, "mongodb deSerialize exception:{}", e::getLocalizedMessage); return null; } }