Getting ClassCastException when consumer consumes userdefined object from solace.

Options

I have published message to the queue as "MessageContent". When i trying to pull messages from queue getting Caused by: java.lang.ClassCastException: [B cannot be cast to solace.test.consumer.entity.MessageContent]

@Bean
public Function<Message, Message> functionUsingTargetDestHeader() {
return input -> {

        String topic="BRV/01";
        String m = input.getPayload().getTrustedTermTradeXml();
        System.out.println("XML INPUT : "+m);
        return MessageBuilder.withPayload(input.getPayload()).setHeader(BinderHeaders.TARGET_DESTINATION, topic).build();
    };
}

**Maven dependencies : **

<properties>
    <sprint-cloud-version>2020.0.4</sprint-cloud-version>
    <solace-spring-cloud-bom.version>2.1.0</solace-spring-cloud-bom.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.solace.spring.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-solace</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2020.0.4</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>com.solace.spring.cloud</groupId>
            <artifactId>solace-spring-cloud-bom</artifactId>
            <version>2.1.0</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>        

Comments

  • marc
    marc Member, Administrator, Moderator, Employee Posts: 923 admin
    Options

    Hi @Monica Gavali ,

    If I read this correctly the app/config in your post is the receiving app that is throwing the error. correct?
    If so, can you share more about the publisher? Is it a non-Spring Cloud Stream app? Maybe using JCSMP or using another API? Also can you share code?

    thanks!

  • hlipperjohn
    hlipperjohn Member Posts: 1
    Options

    Casting is the process of type conversion, which is in Java very common because its a statically typed language. Type Casting only works when the casted object follows an is a relationship to the type you are trying to cast to. ClassCastException is a runtime exception raised in Java when you try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance. It is good practice to guard any explicit casts with an instanceof check first:

    if (myApple instanceof Fruit) {
     Fruit myFruit = (Fruit)myApple;
    }
    

    In general, that's what a cast means: it tells the compiler that even though this assignment might fail, you're pretty certain that it won't. In exchange for allowing the code to compile, you assume the risk of a run-time exception.

    Here are some basic rules to keep in mind when casting variables:

    • Casting an object from a sub class to a super class doesn't require an explicit cast.
    • Casting an object from a super class to a sub class requires an explicit cast.
    • The compiler will not allow casts to unrelated types.

    Even when the code compiles without issue, an exception may be thrown at run time if the object being cast is not actually an instance of that class . This will result in the run time exception ClassCastException.