/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.myth.core.spi.repository;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.dromara.myth.annotation.MythSPI;
import org.dromara.myth.common.bean.adapter.CoordinatorRepositoryAdapter;
import org.dromara.myth.common.bean.entity.MythTransaction;
import org.dromara.myth.common.config.MythConfig;
import org.dromara.myth.common.config.MythRedisConfig;
import org.dromara.myth.common.constant.CommonConstant;
import org.dromara.myth.common.enums.MythStatusEnum;
import org.dromara.myth.common.exception.MythException;
import org.dromara.myth.common.exception.MythRuntimeException;
import org.dromara.myth.common.jedis.JedisClient;
import org.dromara.myth.common.jedis.JedisClientCluster;
import org.dromara.myth.common.jedis.JedisClientSentinel;
import org.dromara.myth.common.jedis.JedisClientSingle;
import org.dromara.myth.common.serializer.ObjectSerializer;
import org.dromara.myth.common.utils.LogUtil;
import org.dromara.myth.common.utils.RepositoryConvertUtils;
import org.dromara.myth.common.utils.RepositoryPathUtils;
import org.dromara.myth.core.spi.MythCoordinatorRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* use redis save mythTransaction log.
*
* @author xiaoyu
*/
@MythSPI("redis")
public class RedisCoordinatorRepository implements MythCoordinatorRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisCoordinatorRepository.class);
private ObjectSerializer objectSerializer;
private JedisClient jedisClient;
private String keyPrefix;
@Override
public int create(final MythTransaction mythTransaction) {
try {
final String redisKey = RepositoryPathUtils.buildRedisKey(keyPrefix, mythTransaction.getTransId());
jedisClient.set(redisKey, RepositoryConvertUtils.convert(mythTransaction, objectSerializer));
return CommonConstant.SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return CommonConstant.ERROR;
}
}
@Override
public int remove(final String transId) {
try {
final String redisKey = RepositoryPathUtils.buildRedisKey(keyPrefix, transId);
return jedisClient.del(redisKey).intValue();
} catch (Exception e) {
e.printStackTrace();
return CommonConstant.ERROR;
}
}
@Override
public int update(final MythTransaction mythTransaction) throws MythRuntimeException {
try {
final String redisKey = RepositoryPathUtils.buildRedisKey(keyPrefix, mythTransaction.getTransId());
mythTransaction.setVersion(mythTransaction.getVersion() + 1);
mythTransaction.setLastTime(new Date());
mythTransaction.setRetriedCount(mythTransaction.getRetriedCount() + 1);
jedisClient.set(redisKey, RepositoryConvertUtils.convert(mythTransaction, objectSerializer));
return CommonConstant.SUCCESS;
} catch (Exception e) {
throw new MythRuntimeException(e);
}
}
@Override
public void updateFailTransaction(final MythTransaction mythTransaction) throws MythRuntimeException {
try {
final String redisKey = RepositoryPathUtils.buildRedisKey(keyPrefix, mythTransaction.getTransId());
mythTransaction.setLastTime(new Date());
jedisClient.set(redisKey, RepositoryConvertUtils.convert(mythTransaction, objectSerializer));
} catch (Exception e) {
throw new MythRuntimeException(e);
}
}
@Override
public void updateParticipant(final MythTransaction mythTransaction) throws MythRuntimeException {
final String redisKey = RepositoryPathUtils.buildRedisKey(keyPrefix, mythTransaction.getTransId());
byte[] contents = jedisClient.get(redisKey.getBytes());
try {
if (contents != null) {
CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(contents, CoordinatorRepositoryAdapter.class);
adapter.setContents(objectSerializer.serialize(mythTransaction.getMythParticipants()));
jedisClient.set(redisKey, objectSerializer.serialize(adapter));
}
} catch (MythException e) {
e.printStackTrace();
throw new MythRuntimeException(e);
}
}
@Override
public int updateStatus(final String id, final Integer status) throws MythRuntimeException {
final String redisKey = RepositoryPathUtils.buildRedisKey(keyPrefix, id);
byte[] contents = jedisClient.get(redisKey.getBytes());
try {
if (contents != null) {
CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(contents, CoordinatorRepositoryAdapter.class);
adapter.setStatus(status);
jedisClient.set(redisKey, objectSerializer.serialize(adapter));
}
} catch (MythException e) {
e.printStackTrace();
throw new MythRuntimeException(e);
}
return CommonConstant.SUCCESS;
}
@Override
public MythTransaction findByTransId(final String transId) {
try {
final String redisKey = RepositoryPathUtils.buildRedisKey(keyPrefix, transId);
byte[] contents = jedisClient.get(redisKey.getBytes());
return RepositoryConvertUtils.transformBean(contents, objectSerializer);
} catch (Exception e) {
throw new MythRuntimeException(e);
}
}
@Override
public List<MythTransaction> listAllByDelay(final Date date) {
final List<MythTransaction> mythTransactionList = listAll();
return mythTransactionList.stream()
.filter(mythTransaction -> mythTransaction.getLastTime().compareTo(date) > 0)
.filter(mythTransaction -> mythTransaction.getStatus() == MythStatusEnum.BEGIN.getCode())
.collect(Collectors.toList());
}
private List<MythTransaction> listAll() {
try {
List<MythTransaction> transactions = Lists.newArrayList();
Set<byte[]> keys = jedisClient.keys((keyPrefix + "*").getBytes());
for (final byte[] key : keys) {
byte[] contents = jedisClient.get(key);
if (contents != null) {
transactions.add(RepositoryConvertUtils.transformBean(contents, objectSerializer));
}
}
return transactions;
} catch (Exception e) {
throw new MythRuntimeException(e);
}
}
@Override
public void init(final String modelName, final MythConfig mythConfig) {
keyPrefix = RepositoryPathUtils.buildRedisKeyPrefix(modelName);
fina
没有合适的资源?快使用搜索试试~ 我知道了~
myth-master.zip
共401个文件
java:261个
xml:75个
yml:17个
0 下载量 108 浏览量
2022-12-14
13:48:40
上传
评论
收藏 1.06MB ZIP 举报
温馨提示
采用消息队列解决分布式事务的开源框架, 基于java语言来开发(JDK1.8),支持dubbo,springcloud,motan等rpc框架进行分布式事务。
资源推荐
资源详情
资源评论
收起资源包目录
myth-master.zip (401个子文件)
.babelrc 220B
spring.factories 145B
com.alibaba.dubbo.rpc.Filter 78B
com.weibo.api.motan.filter.Filter 56B
.gitattributes 27B
.gitignore 379B
.gitignore 43B
index.html 287B
index.html 235B
RedisCoordinatorRepository.java 12KB
JdbcCoordinatorRepository.java 11KB
MongoCoordinatorRepository.java 11KB
CompensationConfiguration.java 10KB
ZookeeperCoordinatorRepository.java 9KB
MythMqReceiveServiceImpl.java 9KB
FileCoordinatorRepository.java 9KB
MythConfig.java 8KB
MythTransactionEngine.java 7KB
ZookeeperLogServiceImpl.java 7KB
FileLogServiceImpl.java 6KB
RedisLogServiceImpl.java 6KB
JdbcLogServiceImpl.java 5KB
MythHystrixConcurrencyStrategy.java 5KB
MotanMythTransactionFilter.java 5KB
ScheduledService.java 5KB
MongoLogServiceImpl.java 5KB
SqlHelper.java 5KB
MythTransactionEventPublisher.java 5KB
MythInvokerInvocationHandler.java 5KB
OkHttpTools.java 4KB
MythCoordinatorServiceImpl.java 4KB
MythAutoConfiguration.java 4KB
JedisClientSentinel.java 4KB
IdWorkerUtils.java 4KB
TransactionLogController.java 4KB
MythSendMessageServiceImpl.java 4KB
RepositoryConvertUtils.java 4KB
JedisClientSingle.java 4KB
MythCoordinatorRepository.java 4KB
VersionUtils.java 4KB
LogUtil.java 4KB
AjaxResponse.java 4KB
RocketmqConsumer.java 4KB
PaymentServiceImpl.java 4KB
MythFeignHandler.java 4KB
RocketmqConsumer.java 4KB
PageHelper.java 4KB
AmqpConfig.java 3KB
PaymentServiceImpl.java 3KB
AmqpConfig.java 3KB
RocketmqConsumer.java 3KB
MythInitServiceImpl.java 3KB
MotanClientConfiguration.java 3KB
RocketmqConsumer.java 3KB
RepositoryPathUtils.java 3KB
PaymentServiceImpl.java 3KB
RocketmqConsumer.java 3KB
RocketmqConsumer.java 3KB
ProducerTest.java 3KB
AmqpConfig.java 3KB
MythCoordinatorService.java 3KB
SwaggerConfig.java 3KB
SwaggerConfig.java 3KB
JedisClient.java 3KB
AccountServiceImpl.java 3KB
SwaggerConfig.java 3KB
OrderServiceImpl.java 3KB
ActorMythTransactionHandler.java 3KB
InventoryServiceImpl.java 3KB
JedisClientCluster.java 3KB
AccountServiceImpl.java 3KB
MotanServerConfiguration.java 3KB
DateUtils.java 3KB
OrderServiceImpl.java 3KB
MythDbConfig.java 3KB
ProtostuffSerializer.java 3KB
Myth.java 3KB
RocketmqSendServiceImpl.java 3KB
RabbitmqSendServiceImpl.java 3KB
AliyunmqConsumer.java 3KB
MythTransactionEventHandler.java 3KB
MotanServerConfiguration.java 3KB
StartMythTransactionHandler.java 3KB
DubboMythTransactionFilter.java 3KB
AliyunmqSendServiceImpl.java 3KB
MotanMythTransactionInterceptor.java 3KB
AdminConfiguration.java 3KB
AliyunmqConsumer.java 3KB
ExtensionLoader.java 3KB
EventTypeEnum.java 2KB
MythFeignBeanPostProcessor.java 2KB
InventoryServiceImpl.java 2KB
MythTransactionFactoryServiceImpl.java 2KB
MythTransaction.java 2KB
AccountServiceImpl.java 2KB
SpringCloudMythTransactionInterceptor.java 2KB
MythTransactionAspectServiceImpl.java 2KB
AuthInterceptor.java 2KB
RepositorySupportEnum.java 2KB
InventoryController.java 2KB
共 401 条
- 1
- 2
- 3
- 4
- 5
资源评论
m0_72731342
- 粉丝: 2
- 资源: 1832
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功