问题
I have this configuration:
1) WSO2 4.7.0 ESB
2) WSO2 MB 2.1.0
3) a topic = MyTopic
4) one subscriber to MyTopic
5) N publishers on MyTopic
6) Static LoadBalance Endpoint deployed on ESB
My goal is that when one of the N endpoints publishes a message on MyTopic, the subscriber on the ESB should be able to add an endpoint to the LoadBalanceEndpoint
list.
Is that possible? Do I need to use DynamicLoadBalanceEndpoint, and if so, how?
回答1:
ok i found the answer by myself. It can be done by accessing the WSO2 registry. You have to save the loadbalance enpoint into registry. Then make reference to these links
1) here is illustrated the way you can access the registry: http://vvratha.blogspot.it/2013/02/accessing-registry-resources-from-class.html
2)here you can find how to convert the OMElment resulting from the regInstance.getResource(resourceKey) into a LoadBalance endpoint https://svn.wso2.org/repos/wso2/carbon/platform/branches/4.0.0/dependencies/synapse/2.1.1-wso2v1/modules/core/src/test/java/org/apache/synapse/config/xml/endpoints/LoadBalanceEndpointSerializationTest.java
3) by this code you can add a new AddressEndpoint to it:
List<Endpoint>list = le.getChildren(); //le is LoadBalanceEndpoint instance
AddressEndpoint ad = new AddressEndpoint();
EndpointDefinition def = new EndpointDefinition();
def.setAddress("http:///your_address_url");
def.setAddressingOn(false);
def.setTimeoutAction(100);
ad.setDefinition(def);
list.add(ad);
le.setChildren(list);
note: if you want to access the loadbalance endpoint and to modify it in memory, use this:
LoadbalanceEndpoint le =(LoadbalanceEndpoint) synapseMsgContext.getConfiguration().getEndpoint("test");
4) After you have added the address point use the
regInstance.updateResource("key", LoadbalanceEndpointSerializer.getElementFromEndpoint(endpoint));
statement to update the registry.
This is the full code for working on the local registry:
Registry regInstance = synapseMsgContext.getConfiguration()
.getRegistry();
Object obj = (Object) regInstance.getResource(new Entry("diogene/diogeneEndpoints.xml"),null);
LoadbalanceEndpoint endpoint = (LoadbalanceEndpoint) LoadbalanceEndpointFactory.getEndpointFromElement((OMElement) obj, false, null);
List<Endpoint>list = endpoint.getChildren();
AddressEndpoint ad = new AddressEndpoint();
EndpointDefinition def = new EndpointDefinition();
def.setAddress("http://your_address_url/");
def.setAddressingOn(false);
def.setTimeoutAction(100);
ad.setDefinition(def);
list.add(ad);
endpoint.setChildren(list);
regInstance.updateResource("key", LoadbalanceEndpointSerializer.getElementFromEndpoint(endpoint));
来源:https://stackoverflow.com/questions/20199200/wso2-dynamically-adding-an-endpoint-to-loadbalance-endpoint