WSO2 Dynamically Adding an EndPoint to LoadBalance Endpoint

强颜欢笑 提交于 2019-12-12 06:47:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!