Axon, Spring, JSF and me - No handler was subscribed to commands of type [NeuerKundeCommand]

Hi

I try to get my own sample running - Axon with Spring - and i'm
running against a wall with my configuration or the implementation (5
lines of code ;-).

org.axonframework.commandhandling.NoHandlerForCommandException: No
handler was subscribed to commands of type [NeuerKundeCommand]

What i'm trying to do is: i want to send a the command
[NeuerKundeCommand] from my JSF Backing Bean. I was thinking, that the
@CommandHandler Annotation and the "<axon:command-bus id="commandBus"/

" is enough for the job... any idea where i should take a look?

Cheers, Reto

@Component("demoBean")
@Scope("session")
public class DemoBean {

    @Autowired
    DemoSpringBeanExample dsbe;

    @Autowired
    CommandBus commandBus;

        public String getMessage() {

          if ( dsbe == null) { return "Merdé - DI not working";}

          if (commandBus == null) { return "HOLYMOLY from DemoBean -
Command Bus is NULL - SI4";}
          commandBus.dispatch( new NeuerKundeCommand("Lanz",
"Thomas"));
          return "Hello World! Comand send ";
        }
}

public class KundeCommandHandler {

    private Repository<Kunde> kundeRepository;

    @CommandHandler
    public void handle(final NeuerKundeCommand command, UnitOfWork
unitOfWork)
            throws Throwable {
        System.out.println("Hier command handler NeuerKunde: " +
command.getName());

        AggregateIdentifier id = new
StringAggregateIdentifier(UUID.randomUUID().toString());

        Kunde kunde = new Kunde(id, command.getName(),
command.getVorname());
        kundeRepository.add(kunde);
    }

    @Required
    public void setKundeRepository(Repository<Kunde> kundeRepository)
{
        this.kundeRepository = kundeRepository;
    }
}

public class NeuerKundeCommand {

    private String name;
    private String vorname;

    public NeuerKundeCommand(String name, String vorname) {
        this.name = name;
        this.vorname = vorname;
    }

    public String getName() {
        return name;
    }

    public String getVorname() {
        return vorname;
    }
}

and that my application-context.xml

    <context:annotation-config/>

    <context:component-scan base-package="ch.fabbri.jsf"/>
    <context:component-scan base-package="ch.fabbri.core"/>

    <axon:annotation-config/>

    <axon:event-bus id="eventBus"/>
    <axon:command-bus id="commandBus"/>

    <bean name="axon:annotationCommandHandlerBeanPostProcessor"

class="org.axonframework.commandhandling.annotation.AnnotationCommandHandlerBeanPostProcessor"/

Hi Reto,

the problem is that the bean defining the @CommandHandler is a session scoped bean. This means that at initialization time, there is no instance of that bean to wire with the command bus. Once a bean is constructed, it is not automatically subscribed. That wouldn’t make sense, since you can have multiple concurrent sessions, but only a single handler per command type.

What you’d need to do is create a singleton bean for the @CommandHandler methods, and call that bean from your session scoped bean.
Note that you don’t need to configure an “AnnotationCommandHandlerBeanPostProcessor” if you also have axon:annotation-config/ in your application context. They do the same thing.

Cheers,

Allard

Thanks a lot Allard, that makes some sense :wink: