Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions aop/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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>org.example</groupId>
<artifactId>aop-cloud-multiBundleDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.18.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.21.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>
</dependencies>

<build>
<finalName>aop-multiBundleDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>org.example.RabbitMQDemo</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
117 changes: 117 additions & 0 deletions aop/java/src/main/java/org/example/RabbitMQApikeyConsumerDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* 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 org.example;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.LongString;
import com.rabbitmq.client.SaslMechanism;
import com.rabbitmq.client.impl.CredentialsProvider;
import com.rabbitmq.client.impl.DefaultCredentialsRefreshService;
import com.rabbitmq.client.impl.LongStringHelper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
* RabbitMQ messaging test.
*/
@Slf4j
public class RabbitMQApikeyConsumerDemo {

private String apikeyHost = "host"; // Replace with your actual RabbitMQ host
private String port = "5671";
private String virtualHost = "aoptest"; // please create the namespace public/aoptest, the namespace should only has one bundle
private String exchange = "ex-fanout";
private String queue = "qu";
private int msgCount = 100;
private DefaultCredentialsRefreshService refreshService;

private Connection getConnection() throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(apikeyHost);
connectionFactory.setPort(Integer.parseInt(port));
connectionFactory.useSslProtocol();
connectionFactory.setVirtualHost(virtualHost);
final String apikeyToken = "apikeyToken"; // Replace with your actual API key token

connectionFactory.setCredentialsProvider(new CredentialsProvider() {
@Override
public String getUsername() {
return null;
}

@Override
public String getPassword() {
return apikeyToken;
}
});
connectionFactory.setSaslConfig(strings -> new SaslMechanism() {
@Override
public String getName() {
return "token";
}

@Override
public LongString handleChallenge(LongString longString, String s, String s1) {
return LongStringHelper.asLongString(s1);
}
});
refreshService = new DefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder().build();
connectionFactory.setCredentialsRefreshService(refreshService);

return connectionFactory.newConnection();
}

public void start() throws Exception {
Connection connection = getConnection();
log.info("get connection");
var channel = connection.createChannel();
String ex = exchange;
String qu = queue;

channel.exchangeDeclare(ex, BuiltinExchangeType.FANOUT, true);
channel.queueDeclare(qu, true, false, false, null);
channel.queueBind(qu, ex, "");
log.info("declared exchange and queue");

CountDownLatch countDownLatch = new CountDownLatch(msgCount);
channel.basicConsume(qu, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
log.info("Received message: {}", new String(body));
countDownLatch.countDown();
}
});
countDownLatch.await();
log.info("consume all messages");

channel.close();
connection.close();
refreshService.close();
}

public static void main(String[] args) throws Exception {
log.info("start consumer demo");
RabbitMQApikeyConsumerDemo demo = new RabbitMQApikeyConsumerDemo();
demo.start();
log.info("finish consumer demo");
}

}
105 changes: 105 additions & 0 deletions aop/java/src/main/java/org/example/RabbitMQApikeyProducerDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* 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 org.example;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.LongString;
import com.rabbitmq.client.SaslMechanism;
import com.rabbitmq.client.impl.CredentialsProvider;
import com.rabbitmq.client.impl.DefaultCredentialsRefreshService;
import com.rabbitmq.client.impl.LongStringHelper;
import lombok.extern.slf4j.Slf4j;

/**
* RabbitMQ messaging test.
*/
@Slf4j
public class RabbitMQApikeyProducerDemo {

private String apikeyHost = "host"; // Replace with your actual RabbitMQ host
private String port = "5671";
private String virtualHost = "aoptest"; // please create the namespace public/aoptest, the namespace should only has one bundle
private String exchange = "ex-fanout";
private String queue = "qu";
private int msgCount = 100;
private DefaultCredentialsRefreshService refreshService;

private Connection getConnection() throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(apikeyHost);
connectionFactory.setPort(Integer.parseInt(port));
connectionFactory.useSslProtocol();
connectionFactory.setVirtualHost(virtualHost);
final String apikeyToken = "apikeyToken"; // Replace with your actual API key token

connectionFactory.setCredentialsProvider(new CredentialsProvider() {
@Override
public String getUsername() {
return null;
}

@Override
public String getPassword() {
return apikeyToken;
}
});
connectionFactory.setSaslConfig(strings -> new SaslMechanism() {
@Override
public String getName() {
return "token";
}

@Override
public LongString handleChallenge(LongString longString, String s, String s1) {
return LongStringHelper.asLongString(s1);
}
});
refreshService = new DefaultCredentialsRefreshService.DefaultCredentialsRefreshServiceBuilder().build();
connectionFactory.setCredentialsRefreshService(refreshService);

return connectionFactory.newConnection();
}

public void start() throws Exception {
Connection connection = getConnection();
log.info("get connection");
var channel = connection.createChannel();
String ex = exchange;
String qu = queue;

channel.exchangeDeclare(ex, BuiltinExchangeType.FANOUT, true);
channel.queueDeclare(qu, true, false, false, null);
channel.queueBind(qu, ex, "");
log.info("declared exchange and queue");

for (int i = 0; i < msgCount; i++) {
channel.basicPublish(ex, "", null, ("msg " + i).getBytes());
}
log.info("published {} messages", msgCount);

channel.close();
connection.close();
refreshService.close();
}

public static void main(String[] args) throws Exception {
log.info("start producer demo");
RabbitMQApikeyProducerDemo demo = new RabbitMQApikeyProducerDemo();
demo.start();
log.info("finish producer demo");
}

}
47 changes: 47 additions & 0 deletions aop/java/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

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.

-->
<!--

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.

-->
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t:%C@%L] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
<!-- <Logger name="org.eclipse.jetty" level="info"/>-->
<!-- <Logger name="io.streamnative" level="info"/>-->
<!-- <Logger name="org.apache.pulsar" level="info"/>-->
<!-- <Logger name="org.apache.bookkeeper" level="info"/>-->
</Loggers>
</Configuration>
Loading