How to thread-safely share an attribute between the beforePhase() and the afterPhase() methods of a PhaseListener?

sp00m

I need to share an attribute between the beforePhase() and the afterPhase() methods of my PhaseListener, for a same JSF request.

Is the following snippet thread-safe?

public class MyPhaseListener implements PhaseListener {

  private MyObject o = null;

  @Override
  public void beforePhase(PhaseEvent event) {
    if (condition) {
      o = new MyObject();
    }
  }

  @Override
  public void afterPhase(PhaseEvent event) {
    if (o != null) {
      o.process();
      o = null;
    }
  }

  @Override
  public PhaseId getPhaseId() {
    return PhaseId.RESTORE_VIEW;
  }

}

If not, what are other solutions?

BalusC

This is definitely not threadsafe. There's only one phase listener instance applicationwide which is shared across multiple requests. Basically, a phase listener is like an @ApplicationScoped managed bean.

Just set it as a context attribute.

public class MyPhaseListener implements PhaseListener {

  @Override
  public void beforePhase(PhaseEvent event) {
    if (condition) {
      event.getFacesContext().setAttribute("o", new MyObject());
    }
  }

  @Override
  public void afterPhase(PhaseEvent event) {
    MyObject o = (MyObject) event.getFacesContext().getAttribute("o");
    if (o != null) {
      o.process();
    }
  }

  @Override
  public PhaseId getPhaseId() {
    return PhaseId.RESTORE_VIEW;
  }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to safely share an array between main thread and background thread

From Dev

How to share behavior between methods

From Dev

How to share a variable between methods?

From Dev

How to share data and methods between activities and widgets

From Dev

How to safely chain methods?

From Dev

How to @Inject in a PhaseListener

From Java

How can I guarantee a thread is safely unlocking a lock upon termination, while all methods already handle it?

From Dev

How to stop std thread safely?

From Dev

How do I share variables between methods in a controller in Rails 4?

From Dev

How to share an object between threads to demonstrate that it is not thread-safe?

From Dev

Share methods between custom directives

From Dev

How do I share class members, safely?

From Dev

Share a variable between main and thread

From Dev

How to share memory in a thread

From Java

How to safely consume Java Streams safely without isFinite() and isOrdered() methods?

From Dev

How to safely consume Java Streams safely without isFinite() and isOrdered() methods?

From Dev

How to change db objects thread safely in django?

From Dev

How to access an array thread safely in Java?

From Dev

How to thread-safely manipulate lists in Java?

From Java

Share attribute between two classes "QWidget"

From Dev

Share a string between methods in C#

From Dev

Share a string between methods in C#

From Dev

php mvc - share methods between models

From Dev

How can I safely share Rails configuration with Ember.js?

From Dev

How can I safely share Rails configuration with Ember.js?

From Dev

How to properly share an object in my controller between methods with ASP .NET MVC?

From Dev

How to share information between Spring controller methods from GET and POST requests?

From Java

Are global variables thread safe in flask? How do I share data between requests?

From Dev

How to safely use [NSTask waitUntilExit] off the main thread?

Related Related

  1. 1

    How to safely share an array between main thread and background thread

  2. 2

    How to share behavior between methods

  3. 3

    How to share a variable between methods?

  4. 4

    How to share data and methods between activities and widgets

  5. 5

    How to safely chain methods?

  6. 6

    How to @Inject in a PhaseListener

  7. 7

    How can I guarantee a thread is safely unlocking a lock upon termination, while all methods already handle it?

  8. 8

    How to stop std thread safely?

  9. 9

    How do I share variables between methods in a controller in Rails 4?

  10. 10

    How to share an object between threads to demonstrate that it is not thread-safe?

  11. 11

    Share methods between custom directives

  12. 12

    How do I share class members, safely?

  13. 13

    Share a variable between main and thread

  14. 14

    How to share memory in a thread

  15. 15

    How to safely consume Java Streams safely without isFinite() and isOrdered() methods?

  16. 16

    How to safely consume Java Streams safely without isFinite() and isOrdered() methods?

  17. 17

    How to change db objects thread safely in django?

  18. 18

    How to access an array thread safely in Java?

  19. 19

    How to thread-safely manipulate lists in Java?

  20. 20

    Share attribute between two classes "QWidget"

  21. 21

    Share a string between methods in C#

  22. 22

    Share a string between methods in C#

  23. 23

    php mvc - share methods between models

  24. 24

    How can I safely share Rails configuration with Ember.js?

  25. 25

    How can I safely share Rails configuration with Ember.js?

  26. 26

    How to properly share an object in my controller between methods with ASP .NET MVC?

  27. 27

    How to share information between Spring controller methods from GET and POST requests?

  28. 28

    Are global variables thread safe in flask? How do I share data between requests?

  29. 29

    How to safely use [NSTask waitUntilExit] off the main thread?

HotTag

Archive