Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RIP-37] Add new APIs for producer #3987

Merged
merged 1 commit into from
Mar 18, 2022
Merged
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
39 changes: 39 additions & 0 deletions apis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->

<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>rocketmq-all</artifactId>
<groupId>org.apache.rocketmq</groupId>
<version>5.0.0-BETA-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>rocketmq-apis</artifactId>
<name>rocketmq-apis ${project.version}</name>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.apache.rocketmq.apis;

import static com.google.common.base.Preconditions.checkNotNull;

import java.time.Duration;

/**
* Common client configuration.
*/
public class ClientConfiguration {
private final String endpoints;
private final SessionCredentialsProvider sessionCredentialsProvider;
private final Duration requestTimeout;
private final boolean enableTracing;

public static ClientConfigurationBuilder newBuilder() {
return new ClientConfigurationBuilder();
}

public ClientConfiguration(String endpoints, SessionCredentialsProvider sessionCredentialsProvider,
Duration requestTimeout, boolean enableTracing) {
this.endpoints = checkNotNull(endpoints, "endpoints should not be null");
this.sessionCredentialsProvider = checkNotNull(sessionCredentialsProvider, "credentialsProvider should not be"
+ " null");
this.requestTimeout = checkNotNull(requestTimeout, "requestTimeout should be not null");
this.enableTracing = enableTracing;
}

public String getEndpoints() {
return endpoints;
}

public SessionCredentialsProvider getCredentialsProvider() {
return sessionCredentialsProvider;
}

public Duration getRequestTimeout() {
return requestTimeout;
}

public boolean isEnableTracing() {
return enableTracing;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.apache.rocketmq.apis;

import static com.google.common.base.Preconditions.checkNotNull;

import java.time.Duration;

/**
* Builder to set {@link ClientConfiguration}.
*/
public class ClientConfigurationBuilder {
private String endpoints;
private SessionCredentialsProvider sessionCredentialsProvider;
private Duration requestTimeout;
private boolean enableTracing;

/**
* Configure the endpoints with which the SDK should communicate.
*
* <p>Endpoints here means address of service, complying with the following scheme(part square brackets is
* optional).
* <p>1. DNS scheme(default): dns:host[:port], host is the host to resolve via DNS, port is the port to return
* for each address. If not specified, 443 is used.
* <p>2. ipv4 scheme: ipv4:address:port[,address:port,...]
* <p>3. ipv6 scheme: ipv6:address:port[,address:port,...]
* <p>4. http/https scheme: http|https://host[:port], similar to DNS scheme, if port not specified, 443 is used.
*
* @param endpoints address of service.
* @return the client configuration builder instance.
*/
public ClientConfigurationBuilder setEndpoints(String endpoints) {
checkNotNull(endpoints, "endpoints should not be not null");
this.endpoints = endpoints;
return this;
}

public ClientConfigurationBuilder setCredentialProvider(SessionCredentialsProvider sessionCredentialsProvider) {
this.sessionCredentialsProvider = checkNotNull(sessionCredentialsProvider, "credentialsProvider should not be "
+ "null");
return this;
}

public ClientConfigurationBuilder setRequestTimeout(Duration requestTimeout) {
this.requestTimeout = checkNotNull(requestTimeout, "requestTimeout should not be not null");
return this;
}

public ClientConfigurationBuilder enableTracing(boolean enableTracing) {
this.enableTracing = enableTracing;
return this;
}

public ClientConfiguration build() {
return new ClientConfiguration(endpoints, sessionCredentialsProvider, requestTimeout, enableTracing);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.apache.rocketmq.apis;

import java.util.Iterator;
import java.util.ServiceLoader;
import org.apache.rocketmq.apis.message.MessageBuilder;
import org.apache.rocketmq.apis.producer.ProducerBuilder;

/**
* Service provider to seek client, which load client according to
* <a href="https://en.wikipedia.org/wiki/Service_provider_interface">Java SPI mechanism</a>.
*/
public interface ClientServiceProvider {
static ClientServiceProvider loadService() {
final ServiceLoader<ClientServiceProvider> loaders = ServiceLoader.load(ClientServiceProvider.class);
final Iterator<ClientServiceProvider> iterators = loaders.iterator();
if (iterators.hasNext()) {
return iterators.next();
}
throw new UnsupportedOperationException("Client service provider not found");
}

/**
* Get the producer builder by current provider.
*
* @return the producer builder instance.
*/
ProducerBuilder newProducerBuilder();

/**
* Get the message builder by current provider.
*
* @return the message builder instance.
*/
MessageBuilder newMessageBuilder();
}
24 changes: 24 additions & 0 deletions apis/src/main/java/org/apache/rocketmq/apis/MessageQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.apache.rocketmq.apis;

public interface MessageQueue {
String getTopic();

String getId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.apache.rocketmq.apis;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Optional;

/**
* Session credentials used in service authentications.
*/
public class SessionCredentials {
private final String accessKey;
private final String accessSecret;
private final String securityToken;

public SessionCredentials(String accessKey, String accessSecret, String securityToken) {
this.accessKey = checkNotNull(accessKey, "accessKey should not be null");
this.accessSecret = checkNotNull(accessSecret, "accessSecret should not be null");
this.securityToken = checkNotNull(securityToken, "securityToken should not be null");
}

public SessionCredentials(String accessKey, String accessSecret) {
this.accessKey = checkNotNull(accessKey, "accessKey should not be null");
this.accessSecret = checkNotNull(accessSecret, "accessSecret should not be null");
this.securityToken = null;
}

public String getAccessKey() {
return accessKey;
}

public String getAccessSecret() {
return accessSecret;
}

public Optional<String> getSecurityToken() {
if (null == securityToken) {
return Optional.empty();
}
return Optional.of(securityToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.apache.rocketmq.apis;

/**
* Abstract provider to provide {@link SessionCredentials}.
*/
public interface SessionCredentialsProvider {
/**
* Get the provided credentials.
*
* @return provided credentials.
*/
SessionCredentials getCredentials();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.apache.rocketmq.apis;

public class StaticSessionCredentialsProvider implements SessionCredentialsProvider {
private final SessionCredentials credentials;

public StaticSessionCredentialsProvider(String accessKey, String accessSecret) {
this.credentials = new SessionCredentials(accessKey, accessSecret);
}

public StaticSessionCredentialsProvider(String accessKey, String accessSecret, String securityToken) {
this.credentials = new SessionCredentials(accessKey, accessSecret, securityToken);
}

@Override
public SessionCredentials getCredentials() {
return credentials;
}
}
Loading