Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.management.InstanceAlreadyExistsException while deploying spring application

<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>

and my health check strategy looks like below

@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;
    }
}

I get below exception

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
like image 620
constantlearner Avatar asked Jul 02 '26 02:07

constantlearner


2 Answers

I got the javax.management.InstanceAlreadyExistsException as part of the stacktrace, however the leading error was 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.

The reason was that I accidentally kept two .ear files in the /deploy folder of my JBOSS server - both the previous and the current .ear of my application.

As it was stated here: https://www.theserverside.com/discussions/thread/22674.html

I'm not sure there is a way to deploy two ear files with the same classes inside and not have problems.

Since the two .ears had classes in common, that lead to InstanceAlreadyExistsException.

like image 149
wile the coyote Avatar answered Jul 04 '26 22:07

wile the coyote


What has worked for me is:

  1. Delete the war file from /webapps
  2. Copy war file from /target and paste into /webapps

Hope it will work for you too.

like image 23
Rohit Suthar Avatar answered Jul 04 '26 22:07

Rohit Suthar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!