[quartznet:4122] Using shared data between multiple instances of a Job type.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[quartznet:4122] Using shared data between multiple instances of a Job type.

Jalal Khordadi
Is it ok to share data like this?

public class UserProcessJob : IJob
{
private static readonly ConcurrentQueue<User> CachedUsers = new ConcurrentQueue<User>();
private static readonly object SyncLock = new object();

public void Execute(IJobExecutionContext context)
{
   
LoadUsersIfRequired();

   
User user;
   
if(CachedUsers.TryDequeue(out user))
   
{
       
// process user
       
ProcessUser(user);
        user
.LastUpdateDate = DateTime.Now;
       
new Db().Save(user);
   
}
   
   
private void LoadUsersIfRequired()
   
{
       
if(CachedUsers.IsEmpty)
       
{
           
lock(SyncLock)
           
{
                 
if(CachedUsers.IsEmpty)
                     
LoadUsers(CachedUsers);
           
}
       
}
   
}
   
   
private void ProcessUser(User user){ ... }

   
private void LoadUsers(ConcurrentQueue<User> users){ ... }
}

Since it's possible to run a job by multiple threads in the same time, is it ok to share data like this?
Do you suggest any better pattern?

--
You received this message because you are subscribed to the Google Groups "Quartz.NET" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/quartznet.
For more options, visit https://groups.google.com/d/optout.