Category Archives: java

WireMock

Some code for WireMock. @Test public void test() throws Exception { String url = “http://localhost:8080/v3”; stubFor(get(urlPathEqualTo(“/v3”)).willReturn(aResponse() .withHeader(“Content-Type”, “application/json”) .withBody(“{ \”id\”: 123, \”name\”: \”John Doe\” }”) .withStatus(200))); stubFor(get(urlPathEqualTo(“/v4”)).willReturn(aResponse() .withHeader(“Content-Type”, “application/json”) .withBody(“{ \”id\”: 123321, \”name\”: \”John Doe\” }”) .withStatus(200))); stubFor(get(urlPathEqualTo(“/v3”)) .withQueryParam(“abc”, equalTo(“5”)) .willReturn(aResponse() .withHeader(“Content-Type”, “application/json”) .withBody(“{ \”id\”: v33, \”name\”: \”John Doe\” }”) .withStatus(200) )); HttpClient client =… Read More »

surefire, failsafe

surefire, failsafe are maven plugin tools. surefire focus on unit test: mvn surefire:test failsafe focus on integration-test: mvn failsafe:integration-test Also need build-helper-maven-plugin to specify the integration test folder <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>${mojo.build-helper-maven-plugin.version}</version> <executions> <execution> <id>add-test-source</id> <phase>generate-test-sources</phase> <goals> <goal>add-test-source</goal> </goals> <configuration> <sources> <source>src/it/java</source> </sources> </configuration> </execution> <execution> <id>add-test-resource</id> <phase>generate-test-resources</phase> <goals> <goal>add-test-resource</goal> </goals> <configuration> <resources> <resource> <directory>src/it/resources</directory> </resource>… Read More »

Add log4j MDC log tags in webflux

Webflux is async. So log4j with MDC tags may not work correctly natively. So adding below to copy MDC context in each operator changes. Hooks is some advanced usage for customizing action in operators. 1. MdcContextLifterConfiguration @Configuration public class MdcContextLifterConfiguration { private static final String MDC_CONTEXT_REACTOR_KEY = MdcContextLifterConfiguration.class.getName(); @PostConstruct private void contextOperatorHook() { Hooks.onEachOperator( MDC_CONTEXT_REACTOR_KEY, Operators.lift((scannable, coreSubscriber)… Read More »

Adding tags for micrometer in WebFlux

Try WebFluxTagsProvider, DefaultWebFluxTagsProvider. @Component @Primary public class ObserveWebFilter extends DefaultWebClientExchangeTagsProvider implements WebFluxTagsProvider { private static final WebFluxTagsProvider WEB_FLUX_TAGS_PROVIDER = new DefaultWebFluxTagsProvider(); @Override public Iterable<Tag> httpRequestTags(ServerWebExchange exchange, Throwable ex) { HttpHeaders headers = exchange.getRequest().getHeaders(); return Tags.concat(WEB_FLUX_TAGS_PROVIDER.httpRequestTags(exchange, ex), ObserveUtils.tags(headers)); } public Iterable<Tag> tags(ClientRequest request, ClientResponse response, Throwable throwable) { HttpHeaders headers = request.headers(); return Tags.concat(super.tags(request, response, throwable), ObserveUtils.tags(headers)); }… Read More »

DynamodDb access in Java(Code)

Assume we have below data in DynamoDb. All fields are String type. PK is ID, SK is name. We use DynamoDbEnhancedAsyncClient to access DynamoDb data. To get a specific record with (PK, SK): public static void testGetItem() throws Exception { CompletableFuture<Record> recordFuture = table.getItem(Key.builder() .partitionValue(“001”) .sortValue(33) .build()); System.out.println(recordFuture.get()); } To query only on PK: public static… Read More »

DynamoDB access in Java(structure)

1. dynamodb vs dynamodb-enhanced 1). software.amazon.awssdk, dynamodb 2). software.amazon.awssdk, dynamodb-enhanced, a high-level library. Can map class to DynamoDb tables. Introducing enhanced DynamoDB client in the AWS SDK for Java v2 2. Client vs Table DynamoDbClient/DynamoDbEnhancedClient is like databases access. Think you know the username/password to the database. DynamoDbTable/DynamoDbAsyncTable is the table you will operate on.… Read More »

dependencyManagement as jar

Without specifying <packaging>xxx</packaging> in pom, the default is jar. For project1, it is deployed to nexus. Then project2 can uses it as normal dependency rather than parent. project2 can still uses the version, scope inside dependencyManagement in project1. See below. project1/pom.xml <?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”> <modelVersion>4.0.0</modelVersion> <groupId>com.test.pengli</groupId> <artifactId>project1</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target>… Read More »

dependencyManagement vs dependencies

dependencies uses the dependencies. dependencyManagement doesn’t include dependencies in project. It is mainly defines dependencies and its version, scope. Sub project specifies parent to it. When sub project defines dependencies, it won’t need to specify version, scope. project1/pom.xml This pom.xml has <packaging>pom</packaging> property. In this case, it can be used as parent in sub project. <?xml version=”1.0″… Read More »

Producer, Consumer with ReentrantLock, Condition

public class PC { public static void main(String[] args) { ReentrantLock lock = new ReentrantLock(); Condition added = lock.newCondition(); Condition removed = lock.newCondition(); int[] resource = new int[1]; Consumer c1 = new Consumer(lock, added, removed, resource); Producer p1 = new Producer(lock, added, removed, resource); Producer p2 = new Producer(lock, added, removed, resource); new Thread(c1).start(); new… Read More »

ConcurrentHashMap

Each segment has multiple entries. Each entry, it starts with a LinkedList. When it has too many, it changes to Black-Red-Tree. When write, if two writes are for different segment, then it allows. When multiple writes to same segment, it uses CAS(compare-and-set) to write and handle conflict. Each operation on segment has a counter. The… Read More »