部署spring应用程序

时间:2016-02-17 20:15:54

标签: spring spring-integration

<int:poller fixed-delay="${fixedDelay}" default="true">
        <int:advice-chain>
            <ref bean="pollAdvise"/>

        </int:advice-chain>
    </int:poller>


    <bean id="pollAdvise" class="org.springframework.integration.scheduling.PollSkipAdvice">
        <constructor-arg ref="healthCheckStrategy"/>

    </bean>



    <bean id="healthCheckStrategy" class="test.main.ServiceHealthCheckPollSkipStrategy">
        <property name="url" value="${url}"/>
        <property name="doHealthCheck" value="${doHealthCheck}"/>
        <property name="restTemplate" ref="restTemplate"/>

    </bean>

    <bean id="restTemplate"
          class="org.springframework.web.client.RestTemplate">
        <constructor-arg ref="requestFactory"/>

    </bean>


    <bean id="requestFactory"
          class="test.BatchClientHttpRequestFactory">
        <constructor-arg ref="verifier"/>

    </bean>

我的健康检查策略如下所示

@ManagedResource
public class ServiceHealthCheckPollSkipStrategy  implements PollSkipStrategy  {


    private volatile boolean skip=false;
    private String url;
    private static String doHealthCheck;
    private RestTemplate restTemplate;

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDoHealthCheck() {
        return doHealthCheck;
    }

    public void setDoHealthCheck(String doHealthCheck) {
        ServiceHealthCheckPollSkipStrategy.doHealthCheck= doHealthCheck;
    }

    @Override
    public boolean skipPoll() {


            if(doHealthCheck.equals("false")){
            return this.skip;
        }
        else if(doHealthCheck.equals("true")){
        List<String> urlList =getUrlfromEncodedString(url);
        for (String url : urlList) {
            boolean status =performHealthCheck(url);
            if(status==false){
            return this.skip = true;    
            }
        }

        return this.skip;
        }
        else {
            throw  new IllegalArgumentException("do health check can be either true or false");
        }

    }

    /**
     * Skip future polls.
     */
    @ManagedOperation
    public void skipPolls() {
        this.skip = true;
    }


    /**
     * Resume polling at the next {@code Trigger} event.
     */
    @ManagedOperation
    public void reset() {
        this.skip = false;
    }


    private List<String>  getUrlfromEncodedString(String urls) throws IllegalArgumentException
    {
        if(urls==null){
            throw new IllegalArgumentException("urls passed is null");
        }
        List<String> urlList = Arrays.asList(urls.split("\\s*,\\s*"));      
        return urlList;
    }


    private boolean performHealthCheck(String url){
        if(url==null){
            throw new IllegalArgumentException("url passed is null");
        }


        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("API-Key", "RTM");

        HttpEntity<String> request = new HttpEntity<String>("parameters", headers);

        ResponseEntity<String> response = restTemplate.
                  exchange(url, HttpMethod.OPTIONS, request, String.class);

         if (response.getStatusCode().toString().equals("200")){
             return  true;
           }

        return false;
    }
}

我得到以下异常

Unable to register MBean [test.main.ServiceHealthCheckPollSkipStrategy@73d4b814] with key 'healthCheckStrategy'; nested exception is javax.management.InstanceAlreadyExistsException: test.main:name=healthCheckStrategy,type=ServiceHealthCheckPollSkipStrategy
    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:625)
    at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:550)
    at org.springframework.jmx.export.MBeanExporter.afterSingletonsInstantiated(MBeanExporter.java:432)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139)
    at org.springframework.xd.module.core.SimpleModule.initialize(SimpleModule.java:213)
    at org.springframework.xd.dirt.module.ModuleDeployer.doDeploy(ModuleDeployer.java:217)
    at org.springframework.xd.dirt.module.ModuleDeployer.deploy(ModuleDeployer.java:200)
    at org.springframework.xd.dirt.server.container.DeploymentListener.deployModule(DeploymentListener.java:365)
    at org.springframework.xd.dirt.server.container.DeploymentListener.deployStreamModule(DeploymentListener.java:334)
    at org.springframework.xd.dirt.server.container.DeploymentListener.onChildAdded(DeploymentListener.java:181)
    at org.springframework.xd.dirt.server.container.DeploymentListener.childEvent(DeploymentListener.java:149)
    at org.apache.curator.framework.recipes.cache.PathChildrenCache$5.apply(PathChildrenCache.java:509)
    at org.apache.curator.framework.recipes.cache.PathChildrenCache$5.apply(PathChildrenCache.java:503)
    at org.apache.curator.framework.listen.ListenerContainer$1.run(ListenerContainer.java:92)
    at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:297)
    at org.apache.curator.framework.listen.ListenerContainer.forEach(ListenerContainer.java:83)
    at org.apache.curator.framework.recipes.cache.PathChildrenCache.callListeners(PathChildrenCache.java:500)
    at org.apache.curator.framework.recipes.cache.EventOperation.invoke(EventOperation.java:35)
    at org.apache.curator.framework.recipes.cache.PathChildrenCache$10.run(PathChildrenCache.java:762)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.management.InstanceAlreadyExistsException: test.main:name=healthCheckStrategy,type=ServiceHealthCheckPollSkipStrategy
    at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
    at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
    at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:678)
    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:615)
    ... 30 m

3 个答案:

答案 0 :(得分:1)

对我有用的是:

  1. 从 /webapps 中删除 war 文件
  2. 从/target复制war文件并粘贴到/webapps

希望它也适用于您。

答案 1 :(得分:0)

这是因为您还声明了另一个与“健康检查策略”相同ID的bean。在另一个春季申请。您可能将这两个spring应用程序放在同一台服务器上。

解决方案1: 在这个spring应用程序中更改bean id或另一个将解决你的问题。

解决方案2: 添加EnableMBeanExport注释并为每个spring应用程序设置不同的defaultDomain

@Configuration
@EnableMBeanExport(defaultDomain="first")
public class BeanGenerator {...}

@Configuration
@EnableMBeanExport(defaultDomain="second")
public class BeanGenerator {...}

答案 2 :(得分:0)

我将javax.management.InstanceAlreadyExistsException作为堆栈跟踪的一部分,但是最主要的错误是java.lang.IllegalStateException

javax.management.InstanceAlreadyExistsException: jboss.deployment:id="jboss.web.deployment:war=/my_application", type=Component already registered.

Caused by: java.lang.IllegalStateException: jboss.web.deployment:war=/my_application is already installed.

原因是我不小心在我的JBOSS服务器的.ear文件夹中保留了两个/deploy文件-我的应用程序的前一个和当前的.ear。

如此处所述:https://www.theserverside.com/discussions/thread/22674.html

  

我不确定是否有办法以相同的方式部署两个耳文件   内部没有问题。

由于两个.ear具有相同的类,因此导致InstanceAlreadyExistsException。