-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
实现全局锁在mongodb下的持久化, 完善 MongodbRepository 的相关方法, 并编写相关测试用例; 修改RedisRep…
…ository的releaseHmilyLocks()方法的返回值 (#360)
- Loading branch information
1 parent
334d863
commit db6faa0
Showing
6 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ry-mongodb/src/main/java/org/dromara/hmily/repository/mongodb/entity/LockMongoEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.dromara.hmily.repository.mongodb.entity; | ||
|
||
import lombok.Data; | ||
import lombok.ToString; | ||
import org.springframework.data.mongodb.core.index.Indexed; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
import org.springframework.data.mongodb.core.mapping.Field; | ||
|
||
|
||
/** | ||
* mongo entity. | ||
* | ||
* @author gcedar | ||
*/ | ||
@Data | ||
@ToString | ||
@Document(collection = "hmily_lock_global") | ||
public class LockMongoEntity { | ||
|
||
@Field("lock_id") | ||
@Indexed | ||
private String lockId; | ||
|
||
@Field("trans_id") | ||
private Long transId; | ||
|
||
@Field("participant_id") | ||
private Long participantId; | ||
|
||
@Field("resource_id") | ||
private String resourceId; | ||
|
||
@Field("target_table_Name") | ||
private String targetTableName; | ||
|
||
@Field("target_table_pk") | ||
private String targetTablePk; | ||
} |
105 changes: 105 additions & 0 deletions
105
...ory-mongodb/src/test/java/org/dromara/hmily/repository/mongodb/MongodbRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package org.dromara.hmily.repository.mongodb; | ||
|
||
import org.dromara.hmily.config.api.ConfigEnv; | ||
import org.dromara.hmily.config.api.entity.HmilyMongoConfig; | ||
import org.dromara.hmily.repository.spi.HmilyRepository; | ||
import org.dromara.hmily.repository.spi.entity.HmilyLock; | ||
import org.dromara.hmily.serializer.spi.HmilySerializer; | ||
import org.dromara.hmily.serializer.spi.exception.HmilySerializerException; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInput; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutput; | ||
import java.io.ObjectOutputStream; | ||
import java.util.*; | ||
|
||
/** | ||
* @Author zhangzhi | ||
* @Date: 2023/8/9 13:33 | ||
*/ | ||
public class MongodbRepositoryTest { | ||
|
||
private MongodbRepository mongodbRepository; | ||
|
||
private final Random random = new Random(System.currentTimeMillis()); | ||
private final List<HmilyLock> locks = new ArrayList<>(); | ||
|
||
@Before | ||
public void setup() { | ||
Long transId = (long) random.nextInt(1000); | ||
Long participantId = (long) random.nextInt(1000); | ||
String resourceId = "jdbc:mysql://localhost:3306/test"; | ||
for (int i = 1; i <= 5; i++) { | ||
HmilyLock lock = new HmilyLock(transId, participantId, resourceId, "tableName" + i, i + ""); | ||
locks.add(lock); | ||
} | ||
registerConfig(); | ||
mongodbRepository = new MongodbRepository(); | ||
mongodbRepository.setSerializer(new HmilySerializer() { | ||
@Override | ||
public byte[] serialize(final Object obj) throws HmilySerializerException { | ||
try (ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); ObjectOutput objectOutput = new ObjectOutputStream(arrayOutputStream)) { | ||
objectOutput.writeObject(obj); | ||
objectOutput.flush(); | ||
return arrayOutputStream.toByteArray(); | ||
} catch (IOException e) { | ||
throw new HmilySerializerException("java serialize error " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T deSerialize(final byte[] param, final Class<T> clazz) throws HmilySerializerException { | ||
try (ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(param); ObjectInput input = new ObjectInputStream(arrayInputStream)) { | ||
return (T) input.readObject(); | ||
} catch (IOException | ClassNotFoundException e) { | ||
throw new HmilySerializerException("java deSerialize error " + e.getMessage()); | ||
} | ||
} | ||
}); | ||
mongodbRepository.init("appName"); | ||
} | ||
|
||
@Test | ||
public void testWriteHmilyLocks() { | ||
int rows = mongodbRepository.writeHmilyLocks(locks); | ||
Assert.assertEquals(locks.size(), rows); | ||
} | ||
|
||
@Test | ||
public void testReleaseHmilyLocks() { | ||
int rows = mongodbRepository.releaseHmilyLocks(locks); | ||
Assert.assertEquals(locks.size(), rows); | ||
} | ||
|
||
@Test | ||
public void testFindHmilyLockById() { | ||
mongodbRepository.writeHmilyLocks(locks); | ||
// 锁不存在 | ||
mongodbRepository.releaseHmilyLocks(locks); | ||
String lockId = locks.get(0).getLockId(); | ||
Optional<HmilyLock> lockOptional = mongodbRepository.findHmilyLockById(lockId); | ||
Assert.assertEquals(Optional.empty(), lockOptional); | ||
|
||
// 锁存在 | ||
mongodbRepository.writeHmilyLocks(locks); | ||
lockId = locks.get(0).getLockId(); | ||
lockOptional = mongodbRepository.findHmilyLockById(lockId); | ||
Assert.assertEquals(lockId, lockOptional.get().getLockId()); | ||
} | ||
|
||
private void registerConfig() { | ||
HmilyMongoConfig mongoConfig = new HmilyMongoConfig(); | ||
mongoConfig.setUrl("localhost:27017"); | ||
mongoConfig.setDatabaseName("hmily"); | ||
mongoConfig.setUserName("test"); | ||
mongoConfig.setPassword("test"); | ||
ConfigEnv.getInstance().registerConfig(mongoConfig); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
hmily-repository/hmily-repository-mongodb/src/test/resources/log4j.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
log4j.rootLogger=INFO,CONSOLE | ||
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender | ||
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.CONSOLE.layout.ConversionPattern=[frame] %d{yyyy-MM-dd HH:mm:ss,SSS} - %-4r %-5p [%t] %C:%L %x - %m%n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters