Axon 3.2.2 to 3.3.2 upgrade issues on JPA entity update

514 views
Skip to first unread message

arun prasanth

unread,
Jul 17, 2018, 5:16:43 AM7/17/18
to Axon Framework Users

Hi,

  In my project we are using AXON 3.2.2 .After upgrading to latest stable 3.3.2 , existing entity update and event firing is not working and below exception is throwing

 As per my understanding a new feature 'scope ' need to added  for messaging on latest release .please help me to understand how we can configure  scope feature in entity for update along with event firing

java.lang.IllegalStateException: Cannot request current Scope if none is active
at org.axonframework.messaging.Scope.getCurrentScope(Scope.java:41) ~[axon-core-3.3.2.jar:3.3.2]
at org.axonframework.commandhandling.model.AggregateLifecycle.getInstance(AggregateLifecycle.java:130) ~[axon-core-3.3.2.jar:3.3.2]
at org.axonframework.commandhandling.model.AggregateLifecycle.apply(AggregateLifecycle.java:67) ~[axon-core-3.3.2.jar:3.3.2]
at com.acme.ecom.product.model.Product.depreciateStock(Product.java:103) ~[classes/:na]


Entity
---------------
@Entity
@Table(name="ECOM_PRODUCT")
public class Product{

private static final long serialVersionUID = 1L;

@Id
         private Integer id;

        @Column(name="PRICE")
        private Double price; 
 
         ------------------------------------
public void depreciateStock(int count){

if(this.stock >= count){

this.stock = this.stock - count;
apply(new StockUpdatedEvent(id, stock));
}else{
throw new RuntimeException("Out of stock");
}
}

         .........................
               ......................
Commnd Handler

 @CommandHandler
    public void handle(NewOrderCommand newOrderCommand) throws Exception{

    AnnotatedAggregate<Product>  productAggregate = productRepository.load(
newOrderCommand.getProductId().toString()).getWrappedAggregate();
    Product product = productAggregate.getAggregateRoot();
    product.depreciateStock(newOrderCommand.getNumber());
....................................
...........................

Steven van Beelen

unread,
Jul 18, 2018, 7:22:29 AM7/18/18
to axonfr...@googlegroups.com
Hi Arun,

You shouldn't have to start a Scope yourself, the framework will take care of that for you.
If you use the regular `Repository<Product>` interface instead of using the repository which gives you the `LockAwareAggregate`, you should be fine.
The `Aggregate<Product>` you'd pull from that has a `invoke(Function<T, R> invocation)` function with which you can call your `depreciateStock()`, which the framework will correctly wrap in a Scope.

Apart from that, I'd suggest to have your `@CommandHandler` annotated functions on the Aggregate directly instead of in a separate component, as the format you're using more so resembles Axon 1 than 2 or 3. Additionally, don't forget to use the `@AggregateRoot` (or `@Aggregate` if you're on Spring) annotation on your Aggregate class.

Hope this helps!

Cheers,
Steven

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages