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

Issue2583 #2614

Merged
merged 7 commits into from
Oct 9, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import org.apache.dubbo.common.utils.ConcurrentHashSet;
import org.apache.dubbo.rpc.Invoker;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static java.util.stream.Collectors.toSet;

// TODO need to adjust project structure in order to fully utilize the methods introduced here.
public class ApplicationModel {

Expand All @@ -36,42 +38,41 @@ public class ApplicationModel {
/**
* full qualified class name -> provided service
*/
private static final ConcurrentMap<String, ProviderModel> providedServices = new ConcurrentHashMap<String, ProviderModel>();
private static final ConcurrentMap<String, Set<ProviderModel>> providedServices = new ConcurrentHashMap<>();
/**
* full qualified class name -> subscribe service
*/
private static final ConcurrentMap<String, ConsumerModel> consumedServices = new ConcurrentHashMap<String, ConsumerModel>();
private static final ConcurrentMap<String, Set<ConsumerModel>> consumedServices = new ConcurrentHashMap<>();

public static final ConcurrentMap<String, Set<Invoker>> providedServicesInvoker = new ConcurrentHashMap<String, Set<Invoker>>();
private static final ConcurrentMap<String, Set<Invoker>> providedServicesInvoker = new ConcurrentHashMap<>();

public static List<ConsumerModel> allConsumerModels() {
return new ArrayList<ConsumerModel>(consumedServices.values());
public static Collection<ConsumerModel> allConsumerModels() {
return consumedServices.values().stream().flatMap(Collection::stream).collect(toSet());
}

public static ProviderModel getProviderModel(String serviceName) {
return providedServices.get(serviceName);
public static Collection<ProviderModel> allProviderModels() {
return providedServices.values().stream().flatMap(Collection::stream).collect(toSet());
}

public static ConsumerModel getConsumerModel(String serviceName) {
return consumedServices.get(serviceName);
public static Collection<ProviderModel> getProviderModel(String serviceName) {
return providedServices.get(serviceName);
}

public static List<ProviderModel> allProviderModels() {
return new ArrayList<ProviderModel>(providedServices.values());
public static Collection<ConsumerModel> getConsumerModel(String serviceName) {
return consumedServices.get(serviceName);
}

public static boolean initConsumerModel(String serviceName, ConsumerModel consumerModel) {
if (consumedServices.putIfAbsent(serviceName, consumerModel) != null) {
public static void initConsumerModel(String serviceName, ConsumerModel consumerModel) {
Set<ConsumerModel> consumerModels = consumedServices.computeIfAbsent(serviceName, k -> new HashSet<>());
if (!consumerModels.add(consumerModel)) {
logger.warn("Already register the same consumer:" + serviceName);
return false;
}
return true;
}

public static void initProviderModel(String serviceName, ProviderModel providerModel) {
if (providedServices.put(serviceName, providerModel) != null) {
Set<ProviderModel> providerModels = providedServices.computeIfAbsent(serviceName, k -> new HashSet<>());
if (!providerModels.add(providerModel)) {
logger.warn("already register the provider service: " + serviceName);
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.dubbo.registry.support.ProviderConsumerRegTable;
import org.apache.dubbo.registry.support.ProviderInvokerWrapper;

import java.util.List;
import java.util.Collection;
import java.util.Set;

@Cmd(name = "offline", summary = "offline dubbo", example = {
Expand All @@ -49,7 +49,7 @@ public String execute(CommandContext commandContext, String[] args) {
}
boolean hasService = false;

List<ProviderModel> providerModelList = ApplicationModel.allProviderModels();
Collection<ProviderModel> providerModelList = ApplicationModel.allProviderModels();
for (ProviderModel providerModel : providerModelList) {
if (providerModel.getServiceName().matches(servicePattern)) {
hasService = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.dubbo.registry.support.ProviderConsumerRegTable;
import org.apache.dubbo.registry.support.ProviderInvokerWrapper;

import java.util.List;
import java.util.Collection;
import java.util.Set;

@Cmd(name = "online", summary = "online dubbo", example = {
Expand All @@ -50,7 +50,7 @@ public String execute(CommandContext commandContext, String[] args) {

boolean hasService = false;

List<ProviderModel> providerModelList = ApplicationModel.allProviderModels();
Collection<ProviderModel> providerModelList = ApplicationModel.allProviderModels();
for (ProviderModel providerModel : providerModelList) {
if (providerModel.getServiceName().matches(servicePattern)) {
hasService = true;
Expand Down