Thanks to visit codestin.com
Credit goes to github.com

Skip to content

URL produces too much garbage objects #3494

@kezhenxu94

Description

@kezhenxu94
  • I have searched the issues of this repository and believe that this is not a duplicate.
  • I have checked the FAQ of this repository and believe that this is not a duplicate.

PROBLEM

Most of the setXxxx and addParameter* in URL such as org.apache.dubbo.common.URL#addParameter and setXxxx will return a new URL object for chaining calls, and most of the "middle objects" are useless and unnecessary, example below:

class org.apache.dubbo.config.spring.SimpleRegistryExporter

    public static Exporter<RegistryService> export(int port, RegistryService registryService) {
        return protocol.export(proxyFactory.getInvoker(registryService, RegistryService.class,
                // HERE
                new URL("dubbo", NetUtils.getLocalHost(), port, RegistryService.class.getName())
                        .setPath(RegistryService.class.getName()) // useless
                        .addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) // useless
                        .addParameter(Constants.CLUSTER_STICKY_KEY, "true") // useless
                        .addParameter(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "1000") // useless
                        .addParameter("ondisconnect", "disconnect") // useless
                        .addParameter("subscribe.1.callback", "true") // useless
                        .addParameter("unsubscribe.1.callback", "false")));
    }

class org.apache.dubbo.config.ServiceConfig

    @SuppressWarnings({"unchecked", "rawtypes"})
    private void exportLocal(URL url) {
        if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
            // HERE
            URL local = URL.valueOf(url.toFullString())
                    .setProtocol(Constants.LOCAL_PROTOCOL) // useless
                    .setHost(LOCALHOST_VALUE) // useless
                    .setPort(0);
            Exporter<?> exporter = protocol.export(
                    proxyFactory.getInvoker(ref, (Class) interfaceClass, local));
            exporters.add(exporter);
            logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry");
        }
    }

SOLUTION

And I suggest refactoring the URL with Builder Pattern. And the possible usage may be:

URL url = ...;
URL newURL = new URLBuilder(url)
.addParameter(k, v)
.addParameter(k2, v2)
.setPath(yourPath)
.build();

Though we cannot remove the original addXXX and setXxxx methods due to compability, we can mark them as deprecated and guide users to use the new URLBuilder.

I'd like to try refactoring this if it's good to the comunity :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions