EventGateway not publishing to EventHandlers?

(Very new to Axon)
I have an command sent to the aggregate that starts the createdEvent. After that starts I have logic that writes to text files. I want a separate event to be published for each file generated.

@NoArgsConstructor
@AllArgsConstructor
@Aggregate
public class SessionAggregate{
    @CommandHandler
    public SessionAggregate(WriteFilesCommand command){
        AggregateLifecycle.apply(new FileStartedEvent(parameters));
    }
    
    @EventSourcingHandler
    public void on(FileStartedEvent event){
        // update aggregate fields
        fileWriter.start(event);
    }

    @EventHandler
    public void on(FileGeneratedEvent event){
        // code that would happen if this method was ever called
    }
}

public class FileWriter {
    // I've tried using autowired annotation as well
    private EventGateway eventGateway;
    
    // Generate Files
    public void start(){
        for() {
            eventGateway.publish(new FileGeneratedEvent(parameters));
        }
    }
}

The FileGeneratedEvent code is never run and the event never appears on the server. I’ve tried moving the @EventHandler method outside of the Aggregate (although I would prefer to keep that logic inside) but that hasn’t helped either.

This is unrelated, but is it possible to publish an event in a different thread?

Any help would be much appreciated!

Welcome to the forum @MKumi36!

I think the issue is you haven’t wired the FileWriter in your SessionAggregate.
However, I’d like to point out a different course altogether.

Axon’s aggregates are, in essence, the “decision-making components” in your framework.
To protect your consistency boundary (a job of the aggregate), Axon will lock an aggregate based on the aggregate identifier for any other operations.

The Command Handling function and any Event Sourcing Handlers that react to events published from the command handler are performed within the lock.
Thus, if you add (outbound) services, those will extend the lock of your aggregate.
Furthermore, this means you generate a potential concurrency issue on the aggregate.

I would thus, as a rule of thumb, state that no aggregate should invoke long-running operations.

I am providing the above insights as I assume your FileWriter isn’t the fastest type of service out there. Hence, I wouldn’t invoke it within the aggregate’s scope.
I would instead adjust your FilerWriter to contain a regular event handling function that generates the files.

By the way, it is OK to publish events outside of an aggregate through the EventGateway.
However, know that as those events are published outside of the lifecycle of an aggregate, those events can never be used to source state in said aggregate.
Aggregates can only handle the events they have published, as the sole events they may handle are used for event sourcing.

I hope this helps you further, @MKumi36!