简体   繁体   中英

Can I set priority of transactional function of a Grails service?

Is there a way to set the priority of a Grails service's functions? Specifically, I have a void transactional function that is very low priority, and I would like it to run only when no other functions require attention... is there a way to do this in Grails?

For example:

@Transactional
class SomeService {
    void nonUrgentFunction() {
    }
    Object functionThatNeedsToHappenFast() {
    }
}

The non-urgent function updates my database with a few objects that are useful for looking back and determining what has happened. But this function can wait. And it is also slow because it requires pessimistic locking, and can be called simultaneously by hundreds of devices hitting my web service.

The urgent function helps in the rendering of a response from my web service, which needs to be fast. If 100 devices are hitting my service concurrently, I need this function to take precedence...

Is there a way set priority for service functions in Grails?

Grails has no concept of a priority in a Service or Service method call. This falls outside the scope of the Grails framework core features.

However, there are many ways to accomplish this. All of which depend upon your specific requirements.

For instance, you could use a ThreadPoolExecutor and a PriorityBlockingQueue to implement your own means of giving priority to specific requests.

You could also use a JMS message broker and messages with various queues and listeners to distribute these requests and manage their priority based on the queue. This would allow you to scale the processing beyond a single JVM.

These are just two examples, there are many other ways to implement this.

While both of these approaches would enable you to accomplish what you are looking to do, they are vastly different when it comes to their implementation and overall capabilities. Ultimately your requirements are going to dictate what type of solution fits your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM