-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: seeflood <[email protected]>
- Loading branch information
Showing
7 changed files
with
208 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>runtime-sdk-parent</artifactId> | ||
<groupId>io.mosn.layotto</groupId> | ||
<version>1.2.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.mosn.layotto</groupId> | ||
<artifactId>examples-lock</artifactId> | ||
<version>1.2.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.mosn.layotto</groupId> | ||
<artifactId>runtime-sdk</artifactId> | ||
<version>1.2.0-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-api</artifactId> | ||
<version>2.9.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>2.9.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
<version>2.9.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>src/main/java</sourceDirectory> | ||
<finalName>examples-lock</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<version>3.2.0</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>io.mosn.layotto.examples.lock.Lock</mainClass> | ||
</manifest> | ||
</archive> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>assemble-all</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
98 changes: 98 additions & 0 deletions
98
examples-lock/src/main/java/io/mosn/layotto/examples/lock/Lock.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,98 @@ | ||
/* | ||
* Copyright 2021 Layotto Authors | ||
* Licensed 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 io.mosn.layotto.examples.lock; | ||
|
||
import io.mosn.layotto.v1.RuntimeClientBuilder; | ||
import io.mosn.layotto.v1.config.RuntimeProperties; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import spec.sdk.runtime.v1.client.RuntimeClient; | ||
import spec.sdk.runtime.v1.domain.lock.*; | ||
|
||
import java.util.UUID; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class Lock { | ||
|
||
private final static Logger logger = LoggerFactory.getLogger(Lock.class); | ||
static String storeName = "lock_demo"; | ||
static String key1 = "key1"; | ||
static String key2 = "key2"; | ||
static String key3 = "key3"; | ||
static String owner1 = UUID.randomUUID().toString(); | ||
static String owner2 = UUID.randomUUID().toString(); | ||
static String owner3 = UUID.randomUUID().toString(); | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
// build RuntimeClient | ||
RuntimeClient client = new RuntimeClientBuilder() | ||
.withPort(RuntimeProperties.DEFAULT_PORT) | ||
.build(); | ||
|
||
// lock key1 successfully | ||
TryLockRequest tryLockRequest = new TryLockRequest(storeName, key1, owner1, 5); | ||
TryLockResponse tryLockResponse = client.tryLock(tryLockRequest); | ||
assertTrue(tryLockResponse.isSuccess()); | ||
logger.info(tryLockResponse.toString()); | ||
|
||
// lock key2 successfully | ||
TryLockRequest tryLockRequest1 = new TryLockRequest(storeName, key2, owner2, 5); | ||
TryLockResponse tryLockResponse1 = client.tryLock(tryLockRequest1); | ||
assertTrue(tryLockResponse1.isSuccess()); | ||
logger.info(tryLockResponse1.toString()); | ||
|
||
// lock key3 successfully | ||
TryLockRequest tryLockRequest2 = new TryLockRequest(storeName, key3, owner3, 3); | ||
TryLockResponse tryLockResponse2 = client.tryLock(tryLockRequest2); | ||
assertTrue(tryLockResponse2.isSuccess()); | ||
logger.info(tryLockResponse2.toString()); | ||
|
||
// unlock key1 successfully | ||
UnlockRequest unlockRequest = new UnlockRequest(storeName, key1, owner1); | ||
UnlockResponse unlockResponse = client.unlock(unlockRequest); | ||
assertEquals(unlockResponse.getStatus(), UnlockResponseStatus.SUCCESS); | ||
logger.info(unlockResponse.toString()); | ||
|
||
// unlock key2 successfully | ||
UnlockRequest unlockRequest1 = new UnlockRequest(storeName, key2, owner2); | ||
UnlockResponse unlockResponse1 = client.unlock(unlockRequest1); | ||
assertEquals(unlockResponse1.getStatus(), UnlockResponseStatus.SUCCESS); | ||
logger.info(tryLockResponse1.toString()); | ||
|
||
TimeUnit.SECONDS.sleep(3); | ||
// unlock key3 failed | ||
UnlockRequest unlockRequest3 = new UnlockRequest(storeName, key3, owner3); | ||
UnlockResponse unlockResponse3 = client.unlock(unlockRequest3); | ||
assertEquals(unlockResponse3.getStatus(), UnlockResponseStatus.LOCK_UNEXIST); | ||
logger.info(unlockResponse3.toString()); | ||
|
||
} | ||
|
||
private static void assertEquals(Object actualResult, Object expected) { | ||
if (actualResult == expected || actualResult.equals(expected)) { | ||
return; | ||
} | ||
String msg = "[Error] Unexpected result:" + actualResult; | ||
System.out.println(msg); | ||
throw new RuntimeException(msg); | ||
} | ||
|
||
private static void assertTrue(boolean condition) { | ||
if (condition) { | ||
return; | ||
} | ||
throw new AssertionError(); | ||
} | ||
} |
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,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="WARN" monitorInterval="30"> | ||
<Properties> | ||
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n</Property> | ||
</Properties> | ||
|
||
<Appenders> | ||
<Console name="console" target="SYSTEM_OUT" follow="true"> | ||
<PatternLayout pattern="${LOG_PATTERN}"/> | ||
</Console> | ||
</Appenders> | ||
|
||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="console"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
59 changes: 0 additions & 59 deletions
59
examples/src/test/java/io/mosn/layotto/examples/lock/Lock.java
This file was deleted.
Oops, something went wrong.
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
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