chenzlalvin commented on code in PR #540: URL: https://github.com/apache/rocketmq-clients/pull/540#discussion_r1223767710
########## java/client/src/main/java/org/apache/rocketmq/client/java/example/ProducerSingleton.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.client.java.example; + +import java.io.IOException; +import org.apache.rocketmq.client.apis.ClientConfiguration; +import org.apache.rocketmq.client.apis.ClientException; +import org.apache.rocketmq.client.apis.ClientServiceProvider; +import org.apache.rocketmq.client.apis.SessionCredentialsProvider; +import org.apache.rocketmq.client.apis.StaticSessionCredentialsProvider; +import org.apache.rocketmq.client.apis.producer.Producer; +import org.apache.rocketmq.client.apis.producer.ProducerBuilder; +import org.apache.rocketmq.client.apis.producer.TransactionChecker; + +public class ProducerSingleton { + private static volatile Producer PRODUCER; + private static final String ACCESS_KEY = "yourAccessKey"; + private static final String SECRET_KEY = "yourSecretKey"; + private static final String ENDPOINTS = "foobar.com:8080"; + + private ProducerSingleton() { + } + + public static Producer getInstance(String... topics) throws ClientException { + return getInstance(null, topics); + } + + public static Producer getInstance(TransactionChecker checker, String... topics) throws ClientException { Review Comment: What happens if getInstance(checker,xxx) is called after getInstance(topic) is called? The checkListener? ########## java/client/src/main/java/org/apache/rocketmq/client/java/example/ProducerSingleton.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.client.java.example; + +import java.io.IOException; +import org.apache.rocketmq.client.apis.ClientConfiguration; +import org.apache.rocketmq.client.apis.ClientException; +import org.apache.rocketmq.client.apis.ClientServiceProvider; +import org.apache.rocketmq.client.apis.SessionCredentialsProvider; +import org.apache.rocketmq.client.apis.StaticSessionCredentialsProvider; +import org.apache.rocketmq.client.apis.producer.Producer; +import org.apache.rocketmq.client.apis.producer.ProducerBuilder; +import org.apache.rocketmq.client.apis.producer.TransactionChecker; + +public class ProducerSingleton { + private static volatile Producer PRODUCER; + private static final String ACCESS_KEY = "yourAccessKey"; + private static final String SECRET_KEY = "yourSecretKey"; + private static final String ENDPOINTS = "foobar.com:8080"; + + private ProducerSingleton() { + } + + public static Producer getInstance(String... topics) throws ClientException { + return getInstance(null, topics); + } + + public static Producer getInstance(TransactionChecker checker, String... topics) throws ClientException { + if (null == PRODUCER) { + synchronized (ProducerSingleton.class) { + if (null == PRODUCER) { + final ClientServiceProvider provider = ClientServiceProvider.loadService(); + // Credential provider is optional for client configuration. Review Comment: Suggest adding detail instructions: This parameter only necessary when the server enables ACL. Otherwise, it does not need to be set by default -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
