use of org.apache.dubbo.rpc.Exporter in project dubbo by alibaba.
the class InjvmProtocolTest method testLocalProtocol.
@Test
public void testLocalProtocol() throws Exception {
DemoService service = new DemoServiceImpl();
Invoker<?> invoker = proxy.getInvoker(service, DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService").addParameter(INTERFACE_KEY, DemoService.class.getName()));
assertTrue(invoker.isAvailable());
Exporter<?> exporter = protocol.export(invoker);
exporters.add(exporter);
service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService").addParameter(INTERFACE_KEY, DemoService.class.getName())));
assertEquals(service.getSize(new String[] { "", "", "" }), 3);
service.invoke("injvm://127.0.0.1/TestService", "invoke");
InjvmInvoker injvmInvoker = new InjvmInvoker(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"), null, new DelegateExporterMap() {
@Override
public boolean isEmpty() {
return true;
}
@Override
public Exporter<?> getExport(String key) {
return null;
}
@Override
public void addExportMap(String key, Exporter<?> exporter) {
}
@Override
public boolean removeExportMap(String key, Exporter<?> exporter) {
return true;
}
@Override
public Collection<Exporter<?>> getExporters() {
return null;
}
});
assertFalse(injvmInvoker.isAvailable());
}
use of org.apache.dubbo.rpc.Exporter in project dubbo by alibaba.
the class CallbackServiceCodec method exportOrUnexportCallbackService.
/**
* export or unexport callback service on client side
*
* @param channel
* @param url
* @param clazz
* @param inst
* @param export
* @throws IOException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static String exportOrUnexportCallbackService(Channel channel, URL url, Class clazz, Object inst, Boolean export) throws IOException {
int instid = System.identityHashCode(inst);
Map<String, String> params = new HashMap<>(3);
// no need to new client again
params.put(IS_SERVER_KEY, Boolean.FALSE.toString());
// mark it's a callback, for troubleshooting
params.put(IS_CALLBACK_SERVICE, Boolean.TRUE.toString());
String group = (url == null ? null : url.getParameter(GROUP_KEY));
if (group != null && group.length() > 0) {
params.put(GROUP_KEY, group);
}
// add method, for verifying against method, automatic fallback (see dubbo protocol)
params.put(METHODS_KEY, StringUtils.join(Wrapper.getWrapper(clazz).getDeclaredMethodNames(), ","));
Map<String, String> tmpMap = new HashMap<>();
if (url != null) {
Map<String, String> parameters = url.getParameters();
if (parameters != null && !parameters.isEmpty()) {
tmpMap.putAll(parameters);
}
}
tmpMap.putAll(params);
// doesn't need to distinguish version for callback
tmpMap.remove(VERSION_KEY);
// callback doesn't needs bind.port
tmpMap.remove(Constants.BIND_PORT_KEY);
tmpMap.put(INTERFACE_KEY, clazz.getName());
URL exportUrl = new URL(DubboProtocol.NAME, channel.getLocalAddress().getAddress().getHostAddress(), channel.getLocalAddress().getPort(), clazz.getName() + "." + instid, tmpMap);
// no need to generate multiple exporters for different channel in the same JVM, cache key cannot collide.
String cacheKey = getClientSideCallbackServiceCacheKey(instid);
String countKey = getClientSideCountKey(clazz.getName());
if (export) {
// one channel can have multiple callback instances, no need to re-export for different instance.
if (!channel.hasAttribute(cacheKey)) {
if (!isInstancesOverLimit(channel, url, clazz.getName(), instid, false)) {
ApplicationModel.getServiceRepository().registerService(clazz);
Invoker<?> invoker = PROXY_FACTORY.getInvoker(inst, clazz, exportUrl);
// should destroy resource?
Exporter<?> exporter = PROTOCOL.export(invoker);
// this is used for tracing if instid has published service or not.
channel.setAttribute(cacheKey, exporter);
logger.info("Export a callback service :" + exportUrl + ", on " + channel + ", url is: " + url);
increaseInstanceCount(channel, countKey);
}
}
} else {
if (channel.hasAttribute(cacheKey)) {
Exporter<?> exporter = (Exporter<?>) channel.getAttribute(cacheKey);
exporter.unexport();
channel.removeAttribute(cacheKey);
decreaseInstanceCount(channel, countKey);
}
}
return String.valueOf(instid);
}
Aggregations