Brief introduction
Many small shops give them a serial number in the approximate order in which they arrive, allowing them to queue in an orderly fashion. The serial number is usually printed on a small piece of paper by a single machine. If multiple customers visit at the same time, the order of queues may be disturbed for politeness reasons.
Disclaimer
The accompanying sample code was created by IBM. The sample code is not part of any standard or IBM product and is provided only to help you develop your application. The code is provided as is and does not provide any form of assurance. IBM is not liable for any damage caused by the use of this sample code, whether or not it prompts for possible loss.
Similar problems often occur in software systems. We need to assign a number to the event, and make sure that the number is unique and follow certain patterns. This problem can be addressed through a number of common solutions, but the distributed system makes the problem more complex. This is very unrealistic if you want to use ECM systems to assign numbers to customers in a small bakery. However, you may need to assign a customer ID, part number, or other simpler number. The database vendor implements a serialized column type for this type of problem. However, P8 does not provide direct access to the database serial number type, so you must use a different mechanism.
In this article, we'll explore how to solve the problem in a P8 environment. We first understand the requirements:
We need to make sure that the assigned number is absolutely unique. It is not acceptable that the same number is assigned two times.
We want the number to follow a pattern to avoid discontinuity in the number allocation. The pattern may contain a lot of things, but for demonstration purposes, this article only uses the simple increment pattern. The next number we get is always greater than the previous number.
We want the number assignment to work reliably in a multi-threaded, multiprocessor, multiple-server, multi-tier, multi-user P8 environment, and achieve exceptional performance.
When we arrive at the bakery's food table, we hope to get a nice little cake of our own.
Before describing the implementation we like, we first look at a few techniques that do not work well. Although you will never need to implement this particular use case, the main points described in this article apply to many P8 programming areas.
Java or. NET Synchronization
If you're new to enterprise development or distributed development, the first thing you can think of is using a large object that is able to synchronize access to the part of the update counter in some way. In Java, this may be a synchronized method or block of code. In C #, this could be a method marked as synchronized or a block of code protected by lock (). A block of code that can be accessed synchronously is sometimes called a critical part. Listing 1 shows one way to implement the code block.
Listing 1. Synchronizing code blocks
/**
* *** DON'T DO IT THIS WAY ***
*/
public class Dispenser
{
/** static access only, so private constructor */
private Dispenser() {}
private static int counter = 0;
public static final synchronized int getNextValue()
{
return ++counter;
}
}