5 Replies Latest reply on Jul 11, 2019 4:31 AM by Yog Sothoth

    ClassCastException when injecting a service from WLF fraction to an app

    Yog Sothoth Newbie

      Since Thorntail community is non-existing and my posts on their Google Group are eternally on moderation, i post this question here, sorry for that.

       

      I've created a custom TT/WLF fraction (org.wildfly.swarm.mymodule) that declares a service interface (MyService) in "api" slot and a service implementation (MyServiceImpl) that is @ApplicationScoped. I then create a simple app that i package as an uberjar and run on a TT instance that has my custom fraction in it. When i try to use CDI and @Inject MyService into a bean in this app i get this exception:

       

      java.lang.ClassCastException: class MyServiceImpl$Proxy$_$$_WeldClientProxy cannot be cast to class org.jboss.weld.bean.proxy.ProxyObject
       (MyServiceImpl$Proxy$_$$_WeldClientProxy is in unnamed module of loader 'org.wildfly.swarm.mymodule:api@1-SNAPSHOT' @61001b64; org.jboss.weld.bean.proxy.ProxyObject is in unnamed module of loader 'org.jboss.weld.core@3.0.5.Final' @63ddc753)

       

      If i comment the @Inject out, everything works fine, so i assume that my fraction and app are both configured and instantiated correctly. I'm familiar with proxying, but not with how Weld does it and what happens when you inject a proxy between [it seems] different modules, so I'm completely lost at this point, and i'd appreciate any insight.

       

      This is my bean in the app

      @ApplicationScoped
      public class MyBean {
       @Inject
       private MyService service;
      
          public void init(@Observes @Initialized(ApplicationScoped.class) Object init) {
      System.out.println("INIT");
          }
          
          @PostConstruct
      public void run() {
      System.out.println("I'M ALIVE!");
       System.out.println(service.getString());
      }
      }

       

      And this is the service in my fraction

      @ApplicationScoped
      public class MyServiceImpl implements MyService {
      private String stuff = "stuff";
      
      @PostConstruct
      public void initialize() {
      System.out.println("INITIALIZED");
      }
      
      @Override
      public String getString() {
      return this.stuff;
      }
      }

       

      beans.xml in both of them is essentially the same:

      <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
             bean-discovery-mode="annotated">
      </beans>
      
      
        • 1. Re: ClassCastException when injecting a service from WLF fraction to an app
          Jaikiran Pai Master

          zam0th  wrote:

           

          Since Thorntail community is non-existing and my posts on their Google Group are eternally on moderation, i post this question here, sorry for that.

          Not sure if you are aware about this recent announcement Thorntail Community Announcement on Quarkus | Thorntail

          zam0th  wrote:

           

          When i try to use CDI and @Inject MyService into a bean in this app i get this exception:

           

           

          java.lang.ClassCastException: class MyServiceImpl$Proxy$_$$_WeldClientProxy cannot be cast to class org.jboss.weld.bean.proxy.ProxyObject  (MyServiceImpl$Proxy$_$$_WeldClientProxy is in unnamed module of loader 'org.wildfly.swarm.mymodule:api@1-SNAPSHOT' @61001b64; org.jboss.weld.bean.proxy.ProxyObject is in unnamed module of loader 'org.jboss.weld.core@3.0.5.Final' @63ddc753)

          Can you post the complete exception stacktrace? That can sometimes give enough hints to understand what's going on.

          • 2. Re: ClassCastException when injecting a service from WLF fraction to an app
            Yog Sothoth Newbie

            Damn, you bet i haven't seen this announcement. I guess i must go and study Quarkius then.

             

            Here's the full stacktrace:

             

            2019-07-08 10:27:14,597 ERROR [org.jboss.threads.errors] (ServerService Thread Pool -- 2) Thread Thread[ServerService Thread Pool -- 2,5,ServerService ThreadGroup] threw an uncaught exception: org.jboss.weld.exceptions.WeldException: WELD-000049: Unable to invoke public void MyApp.run() on MyApp@6960fcc9
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.invokeMethods(DefaultLifecycleCallbackInvoker.java:85)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.postConstruct(DefaultLifecycleCallbackInvoker.java:66)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.injection.producer.BasicInjectionTarget.postConstruct(BasicInjectionTarget.java:122)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:172)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.contexts.AbstractContext.get(AbstractContext.java:96)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.ContextualInstanceStrategy$ApplicationScopedContextualInstanceStrategy.get(ContextualInstanceStrategy.java:140)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:67)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.event.ObserverMethodImpl.getReceiver(ObserverMethodImpl.java:360)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.event.ObserverMethodImpl.getReceiverIfExists(ObserverMethodImpl.java:347)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:305)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:286)
                    at javax.enterprise.api@2.0.SP1//javax.enterprise.inject.spi.ObserverMethod.notify(ObserverMethod.java:124)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.util.Observers.notify(Observers.java:166)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.event.ObserverNotifier.notifySyncObservers(ObserverNotifier.java:285)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.event.ObserverNotifier.notify(ObserverNotifier.java:273)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:177)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bootstrap.BeanDeploymentModule.fireEvent(BeanDeploymentModule.java:93)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bootstrap.WeldStartup.endInitialization(WeldStartup.java:570)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bootstrap.WeldBootstrap.endInitialization(WeldBootstrap.java:100)
                    at org.jboss.as.weld@15.0.1.Final//org.jboss.as.weld.WeldStartCompletionService$1.run(WeldStartCompletionService.java:108)
                    at org.jboss.threads@2.3.2.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
                    at org.jboss.threads@2.3.2.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
                    at org.jboss.threads@2.3.2.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
                    at org.jboss.threads@2.3.2.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
                    at java.base/java.lang.Thread.run(Thread.java:834)
                    at org.jboss.threads@2.3.2.Final//org.jboss.threads.JBossThread.run(JBossThread.java:485)
            Caused by: java.lang.reflect.InvocationTargetException
                    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.injection.producer.DefaultLifecycleCallbackInvoker.invokeMethods(DefaultLifecycleCallbackInvoker.java:83)
                    ... 26 more
            Caused by: java.lang.ClassCastException: class MyServiceImpl$Proxy$_$$_WeldClientProxy cannot be cast to class org.jboss.weld.bean.proxy.ProxyObject (MyServiceImpl$Proxy$_$$_WeldClientProxy is in unnamed module of loader 'org.wildfly.swarm.mymodule:api@1-SNAPSHOT' @61001b64; org.jboss.weld.bean.proxy.ProxyObject is in unnamed module of loader 'org.jboss.weld.core@3.0.5.Final' @63ddc753)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.proxy.ProxyFactory.create(ProxyFactory.java:317)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.proxy.ClientProxyFactory.create(ClientProxyFactory.java:83)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:205)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:195)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.proxy.ClientProxyProvider.access$100(ClientProxyProvider.java:44)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.proxy.ClientProxyProvider$CreateClientProxy.apply(ClientProxyProvider.java:52)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.proxy.ClientProxyProvider$CreateClientProxy.apply(ClientProxyProvider.java:48)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.util.cache.ReentrantMapBackedComputingCache.lambda$null$0(ReentrantMapBackedComputingCache.java:55)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.util.LazyValueHolder$1.computeValue(LazyValueHolder.java:32)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.util.LazyValueHolder.get(LazyValueHolder.java:46)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.util.cache.ReentrantMapBackedComputingCache.getValue(ReentrantMapBackedComputingCache.java:72)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.util.cache.ReentrantMapBackedComputingCache.getCastValue(ReentrantMapBackedComputingCache.java:78)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientProxyProvider.java:229)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:694)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.builtin.InstanceImpl.getBeanInstance(InstanceImpl.java:252)
                    at org.jboss.weld.core@3.0.5.Final//org.jboss.weld.bean.builtin.InstanceImpl.get(InstanceImpl.java:114)
                    at deployment.myapp-0.0.1-SNAPSHOT.jar//MyApp.run(MyApp.java:29)
                    ... 31 more
            • 3. Re: ClassCastException when injecting a service from WLF fraction to an app
              Jaikiran Pai Master

              If you still want to continue with Thorntail, then based on what I see in that stacktrace, I am guessing that you are probably pacakging weld jar/classes within that deployment. Maybe you should mark the weld dependencies in your application's pom.xml as provided so that those dont' get packaged within the app.

              • 4. Re: ClassCastException when injecting a service from WLF fraction to an app
                Yog Sothoth Newbie

                Hmmm, that shouldn't be the case, how do i check whether that's true: examining m2repo in the resulting uberjar? Nowhere do i have an explicit dependency on Weld.

                 

                mymodule pom.xml:

                 

                <project xmlns="http://maven.apache.org/POM/4.0.0"

                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

                <modelVersion>4.0.0</modelVersion>

                <groupId>com.acme.modules</groupId>

                <artifactId>mymodule</artifactId>

                <version>1-SNAPSHOT</version>

                 

                <properties>

                <version.thorntail>2.4.0.Final</version.thorntail>

                <version.thorntail.fraction-plugin>87</version.thorntail.fraction-plugin>

                </properties>

                 

                <dependencyManagement>

                <dependencies>

                <dependency>

                <groupId>io.thorntail</groupId>

                <artifactId>bom</artifactId>

                <version>${version.thorntail}</version>

                <type>pom</type>

                <scope>import</scope>

                </dependency>

                </dependencies>

                </dependencyManagement>

                 

                <dependencies>

                <dependency>

                <groupId>io.thorntail</groupId>

                        <artifactId>undertow</artifactId>

                </dependency>

                <dependency>

                <groupId>io.thorntail</groupId>

                <artifactId>logging</artifactId>

                </dependency>

                <dependency>

                <groupId>io.thorntail</groupId>

                <artifactId>spi</artifactId>

                </dependency>

                <dependency>

                <groupId>javax.enterprise</groupId>

                <artifactId>cdi-api</artifactId>

                <version>2.0</version>

                <scope>provided</scope>

                </dependency>

                <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-context</artifactId>

                <version>4.3.16.RELEASE</version>

                </dependency>

                </dependencies>

                 

                <build>

                <plugins>

                <plugin>

                <groupId>io.thorntail</groupId>

                <artifactId>thorntail-fraction-plugin</artifactId>

                <version>${version.thorntail.fraction-plugin}</version>

                <executions>

                <execution>

                <phase>process-classes</phase>

                <goals>

                <goal>process</goal>

                </goals>

                </execution>

                </executions>

                </plugin>

                <plugin>

                <artifactId>maven-compiler-plugin</artifactId>

                <configuration>

                <source>1.8</source>

                <target>1.8</target>

                </configuration>

                </plugin>

                </plugins>

                </build>

                </project>

                myapp pom.xml:

                 

                <project xmlns="http://maven.apache.org/POM/4.0.0"

                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

                <modelVersion>4.0.0</modelVersion>

                <groupId>com.acme.mystuff</groupId>

                <artifactId>myapp</artifactId>

                <version>0.0.1-SNAPSHOT</version>

                <!-- <packaging>war</packaging> -->

                <properties>

                <version.wildfly.swarm>2.4.0.Final</version.wildfly.swarm>

                    <failOnMissingWebXml>false</failOnMissingWebXml>

                </properties>

                <dependencyManagement>

                <dependencies>

                <dependency>

                <groupId>io.thorntail</groupId>

                <artifactId>bom</artifactId>

                <version>${version.wildfly.swarm}</version>

                <scope>import</scope>

                <type>pom</type>

                </dependency>

                </dependencies>

                </dependencyManagement>

                <dependencies>

                <dependency>

                <groupId>com.acme.modules</groupId>

                <artifactId>mymodule</artifactId>

                <version>1-SNAPSHOT</version>

                </dependency>

                <dependency>

                <groupId>javax</groupId>

                <artifactId>javaee-api</artifactId>

                <version>7.0</version>

                <scope>provided</scope>

                </dependency>

                <dependency>

                <groupId>javax.enterprise</groupId>

                <artifactId>cdi-api</artifactId>

                <version>2.0</version>

                <scope>provided</scope>

                </dependency>

                </dependencies>

                <build>

                <plugins>

                <plugin>

                <groupId>io.thorntail</groupId>

                <artifactId>thorntail-maven-plugin</artifactId>

                <version>${version.wildfly.swarm}</version>

                <executions>

                <execution>

                <goals>

                <goal>package</goal>

                </goals>

                <configuration>

                <fractionDetectMode>force</fractionDetectMode>

                </configuration>

                </execution>

                </executions>

                </plugin>

                <plugin>

                <artifactId>maven-compiler-plugin</artifactId>

                <configuration>

                <source>1.8</source>

                <target>1.8</target>

                </configuration>

                </plugin>

                </plugins>

                </build>

                </project>

                • 5. Re: ClassCastException when injecting a service from WLF fraction to an app
                  Yog Sothoth Newbie

                  Bump. I don't depend on Weld anywhere, so it must be something else.