Giter Site home page Giter Site logo

Comments (7)

alturkovic avatar alturkovic commented on June 16, 2024

Could you explain in more details what the issue is please? What kind of problems occur with @Async and @Locked?

from distributed-lock.

chenlingmin avatar chenlingmin commented on June 16, 2024

He will cause the lock to be implemented first, and then to enable asynchronous threads.

This will invalidate the lock.

  |        +-------------------------------------------+
  |        | Lock:Thread-1                             |
  |        |    +-----------------------------------+  |
  |        |    | Async:Thread-2                    |  |
  |        |    |   +----------------------------+  |  |
  |        |    |   | method:Thread-2            |  |  |
  |        |    |   +----------------------------+  |  |
  |        |    +-----------------------------------+  |
  |        +-------------------------------------------+
  ↓

from distributed-lock.

alturkovic avatar alturkovic commented on June 16, 2024

Sorry for the late reply. You can extract the logic to be locked in a new service and annotate that method using @RedisLocked instead. That behaviour is pretty similar to @Transactional behaviour to control transactional logic so I am not sure this requires any changes since it can be mitigated.

from distributed-lock.

chenlingmin avatar chenlingmin commented on June 16, 2024

Currently, I have changed the Order value of LockBeanPostProcessor so that it can work before AsyncAnnotationBeanPostProcessor, This solution works.

Like this

  @Bean
    public LockBeanPostProcessor lockBeanPostProcessor(
            final ConfigurableBeanFactory configurableBeanFactory,
            final KeyGenerator keyGenerator,
            @Autowired(required = false) final TaskScheduler taskScheduler) {
            LockBeanPostProcessor lockBeanPostProcessor  = new LockBeanPostProcessor(new BeanFactoryAwareIntervalConverter(configurableBeanFactory), configurableBeanFactory::getBean, keyGenerator, taskScheduler);
            lockBeanPostProcessor.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
            return lockBeanPostProcessor;
    }

But this method is not elegant enough, it is best to set LockBeanPostProcessor according to whether there is Transactional or Async

from distributed-lock.

alturkovic avatar alturkovic commented on June 16, 2024

I understand your point, but I think you already noticed yourself that it won't really be possible to get the right order for each application. According to a comment here: Spring project's aspects do not have advice ordering guaranteed. And this is the same argument I am making.

How about something like this:


@Autowired
private LockedService lockedService;

@EventListener
@Async
public void sendEmail(XXXEvent event) {
    lockedService.sendEmail(event);
}

class LockedService {

  @RedisLocked
  public void sendEmail(XXXEvent event) {
  }

}

from distributed-lock.

chenlingmin avatar chenlingmin commented on June 16, 2024

Thank you, your solution is better, but users need to pay attention to the order of aspects.

Expect Spring to provide a more systematic solution.😂

from distributed-lock.

alturkovic avatar alturkovic commented on June 16, 2024

I think we reached an acceptable conclusion so I am going to close this issue for now.

from distributed-lock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.