-
Notifications
You must be signed in to change notification settings - Fork 105
/
Counter.java
40 lines (33 loc) · 985 Bytes
/
Counter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.jboss.narayana.quickstarts.jta;
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;
import jakarta.transaction.TransactionScoped;
/**
* <p>
* Transactional scoped counter.<br/>
* Data of the counter are stored along the existence
* of the particular transaction.
* </p>
* <p>
* With starting a new transaction the injected counter
* is initiated as a new instance.
* When transaction finishes the counter is cleared up.
* </p>
*
* @author <a href="mailto:gytis@redhat.com">Gytis Trikleris</a>
*/
@TransactionScoped
public class Counter implements Serializable {
private static final long serialVersionUID = 1L;
private final AtomicInteger counter = new AtomicInteger();
public int get() {
return counter.get();
}
public void increment() {
counter.incrementAndGet();
}
@Override
public String toString() {
return this.hashCode() + "[value:" + counter.get() + "]";
}
}