下面列出了怎么用javax.ejb.Lock的API类实例代码及写法,或者点击链接到github查看源代码。
@Lock(LockType.WRITE)
public Order checkout() {
final Order order = new Order();
order.getSelections().addAll(shoppingCart.getSelections());
order.setPrice(priceCalculator.calculateTotal(order.getSelections()));
final long id = orders.size() + 1;
order.setId(id);
order.setDate(LocalDateTime.now());
order.setStatus(OrderStatus.CONFIRMED);
shoppingCart.clear();
orders.put(id, order);
return order;
}
@Lock(LockType.WRITE)
public Order checkout() {
final Order order = new Order();
order.getSelections().addAll(shoppingCart.getSelections());
order.setPrice(priceCalculator.calculateTotal(order.getSelections()));
final long id = orders.size() + 1;
order.setId(id);
order.setDate(LocalDateTime.now());
order.setStatus(OrderStatus.CONFIRMED);
shoppingCart.clear();
orders.put(id, order);
return order;
}
@Schedule(hour = "3")
@Lock(LockType.WRITE)
private void refresh() {
List<MDSService> newList = new ArrayList<>();
for (MDSService service : mdsList) {
try {
service.refresh();
newList.add(service);
} catch (Exception e) {
// if there's an exception, it won't get added to the new list
}
}
mdsList = newList;
}
@GET
@Lock(READ)
@Produces(MediaType.SERVER_SENT_EVENTS)
public void itemEvents(@HeaderParam(HttpHeaders.LAST_EVENT_ID_HEADER)
@DefaultValue("-1") int lastEventId,
@Context SseEventSink eventSink) {
if (lastEventId >= 0)
replayLastMessages(lastEventId, eventSink);
sseBroadcaster.register(eventSink);
}
@Lock(WRITE)
public void onEvent(@Observes DomainEvent domainEvent) {
String message = domainEvent.getContents();
messages.add(message);
OutboundSseEvent event = createEvent(message, ++lastEventId);
sseBroadcaster.broadcast(event);
}
@Lock(LockType.READ)
@Produces
@ApplicationScoped
public JestClient produce() {
LOGGER.log(Level.INFO, "Producing ElasticSearch rest client");
return client;
}
@Lock(LockType.WRITE)
public void addBookSelection(BookSelection selection) {
final Optional<BookSelection> existentSelection = selections.stream().filter(s -> s.getBook().equals(selection.getBook())).findFirst();
if (existentSelection.isPresent()) {
final BookSelection bookSelection = existentSelection.get();
bookSelection.setQuantity(bookSelection.getQuantity() + selection.getQuantity());
updatePrice(bookSelection);
} else {
updatePrice(selection);
selections.add(selection);
selection.setId(nextSelectionId++);
}
}
@Lock(LockType.WRITE)
public void updateBookSelection(long selectionId, int quantity) {
final BookSelection selection = selections.stream()
.filter(s -> s.getId() == selectionId).findFirst()
.orElseThrow(() -> new IllegalArgumentException("No selection found"));
selection.setQuantity(quantity);
updatePrice(selection);
if (quantity == 0)
selections.remove(selection);
}
@Lock(LockType.READ)
public List<Order> getOrders() {
final ArrayList<Order> orders = new ArrayList<>(this.orders.values());
orders.sort(Comparator.comparing(Order::getId));
return orders;
}
@Lock(LockType.WRITE)
public void addBookSelection(BookSelection selection) {
final Optional<BookSelection> existentSelection = selections.stream().filter(s -> s.getBook().equals(selection.getBook())).findFirst();
if (existentSelection.isPresent()) {
final BookSelection bookSelection = existentSelection.get();
bookSelection.setQuantity(bookSelection.getQuantity() + selection.getQuantity());
updatePrice(bookSelection);
} else {
updatePrice(selection);
selections.add(selection);
selection.setId(nextSelectionId++);
}
}
@Lock(LockType.WRITE)
public void updateBookSelection(long selectionId, int quantity) {
final BookSelection selection = selections.stream()
.filter(s -> s.getId() == selectionId).findFirst()
.orElseThrow(() -> new IllegalArgumentException("No selection found"));
selection.setQuantity(quantity);
updatePrice(selection);
if (quantity == 0)
selections.remove(selection);
}
@Lock(LockType.READ)
public List<Order> getOrders() {
final ArrayList<Order> orders = new ArrayList<>(this.orders.values());
orders.sort(Comparator.comparing(Order::getId));
return orders;
}
@Lock(LockType.WRITE)
public void addBookSelection(BookSelection selection) {
final Optional<BookSelection> existentSelection = selections.stream().filter(s -> s.getBook().equals(selection.getBook())).findFirst();
if (existentSelection.isPresent()) {
final BookSelection bookSelection = existentSelection.get();
bookSelection.setQuantity(bookSelection.getQuantity() + selection.getQuantity());
updatePrice(bookSelection);
} else {
updatePrice(selection);
selections.add(selection);
selection.setId(nextSelectionId++);
}
}
@Lock(LockType.WRITE)
public void updateBookSelection(long selectionId, int quantity) {
final BookSelection selection = selections.stream()
.filter(s -> s.getId() == selectionId).findFirst()
.orElseThrow(() -> new IllegalArgumentException("No selection found"));
selection.setQuantity(quantity);
updatePrice(selection);
if (quantity == 0)
selections.remove(selection);
}
@Lock(LockType.READ)
public List<Order> getOrders() {
final ArrayList<Order> orders = new ArrayList<>(this.orders.values());
orders.sort(Comparator.comparing(Order::getId));
return orders;
}
@Schedule(minute = "*/10")
@Lock(LockType.WRITE)
public void refreshCache() {
cache = new HashMap<>();
for (ConfigurationSetting configurationSetting : getAllConfigurationSettings()) {
addToCache(configurationSetting);
}
}
@Override
@Lock(LockType.WRITE)
public void setConfigurationSetting(String informationId, String value) {
ConfigurationSetting configSetting = new ConfigurationSetting(
ConfigurationKey.valueOf(informationId),
Configuration.GLOBAL_CONTEXT, value);
setConfigurationSetting(configSetting);
}
@Lock(LockType.READ)
public void readerMethod(int threadId) {
System.out.println("Enter READER " + threadId + ". R:" + readerCount.incrementAndGet() + "/w:"
+ writerCount.get());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Exit READER " + threadId + ". R:" + readerCount.decrementAndGet() + "/w:"
+ writerCount.get());
}
@Lock(LockType.WRITE)
public void writerMethod(int threadId) {
System.out.println("Entering WRITER " + threadId + ". R:" + readerCount.get() + "/w:"
+ writerCount.incrementAndGet());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Exiting WRITER " + threadId + ". R:" + readerCount.get() + "/w:"
+ writerCount.decrementAndGet());
}
@Lock(WRITE)
public void buyTicket(int seatId) throws SeatBookedException, NoSuchSeatException {
final Seat seat = getSeat(seatId);
if (seat.isBooked()) {
throw new SeatBookedException("Seat " + seatId + " already booked!");
}
addSeat(seat.getBookedSeat());
}
@Lock(READ)
private Seat getSeat(int seatId) throws NoSuchSeatException {
final Seat seat = seats.get(seatId);
if (seat == null) {
throw new NoSuchSeatException("Seat " + seatId + " does not exist!");
}
return seat;
}
@Lock(WRITE)
public void buyTicket(int seatId) {
final Seat seat = getSeat(seatId);
if (seat.isBooked()) {
throw new IllegalArgumentException("Seat is already booked: " + seatId);
}
final Seat bookedSeat = seat.getBookedSeat();
addSeat(bookedSeat);
seatEvent.fire(bookedSeat);
}
@Lock(WRITE)
public void buyTicket(int seatId) {
final Seat seat = getSeat(seatId);
if (seat.isBooked()) {
throw new IllegalArgumentException("Seat is already booked: " + seatId);
}
final Seat bookedSeat = seat.getBookedSeat();
addSeat(bookedSeat);
seatEvent.fire(bookedSeat);
}
@Lock(WRITE)
public void buyTicket(int seatId) {
final Seat seat = getSeat(seatId);
if (seat.isBooked()) {
throw new IllegalArgumentException("Seat is already booked: " + seatId);
}
final Seat bookedSeat = seat.getBookedSeat();
addSeat(bookedSeat);
seatEvent.fire(bookedSeat);
}
@Lock(READ)
private Seat getSeat(int seatId) throws NoSuchSeatException {
final Seat seat = seats.get(seatId);
if (seat == null) {
throw new NoSuchSeatException("Seat " + seatId + " does not exist!");
}
return seat;
}
@Lock(WRITE)
public void buyTicket(int seatId) {
final Seat seat = getSeat(seatId);
final Seat bookedSeat = seat.getBookedSeat();
addSeat(bookedSeat);
seatEvent.fire(bookedSeat);
}
@Lock(WRITE)
public void buyTicket(int seatId) {
final Seat seat = getSeat(seatId);
final Seat bookedSeat = seat.getBookedSeat();
addSeat(bookedSeat);
seatEvent.fire(bookedSeat);
}
@Lock(WRITE)
public void buyTicket(int seatId) {
final Seat seat = getSeat(seatId);
if (seat.isBooked()) {
throw new IllegalArgumentException("Seat is already booked: " + seatId);
}
final Seat bookedSeat = seat.getBookedSeat();
addSeat(bookedSeat);
seatEvent.fire(bookedSeat);
}
@Override
@Lock(LockType.READ)
public JsonObject getTrustAnchors(String aaguid, List<String> allowedStatusList) {
JsonObjectBuilder ret = Json.createObjectBuilder();
JsonArrayBuilder errors = Json.createArrayBuilder();
JsonObjectBuilder error = Json.createObjectBuilder();
MetadataTOCPayloadEntry entry = null;
MetadataStatement st = null;
for (MDSService service : mdsList) {
entry = service.getTOCEntry(aaguid);
if (entry!=null) {
for (StatusReport status : entry.getStatusReports()) {
if (status.getStatus()==AuthenticatorStatus.ATTESTATION_KEY_COMPROMISE ||
status.getStatus()==AuthenticatorStatus.REVOKED ||
status.getStatus()==AuthenticatorStatus.USER_KEY_PHYSICAL_COMPROMISE ||
status.getStatus()==AuthenticatorStatus.USER_KEY_REMOTE_COMPROMISE ||
status.getStatus()==AuthenticatorStatus.USER_VERIFICATION_BYPASS
) {
error.add("message", "Authenticator status = "+status.getStatus().name());
errors.add(error);
}
else if(!allowedStatusList.contains(status.getStatus().name())){
error.add("message", "Authenticator status = " + status.getStatus().name() + "not allowed by policy");
errors.add(error);
}
}
}
st = service.getMetadataStatement(aaguid);
if (st != null) {
List<String> attestationRootCertificates = st.getAttestationRootCertificates();
if (attestationRootCertificates != null) {
JsonArrayBuilder certs = Json.createArrayBuilder();
for (String c : attestationRootCertificates) {
certs.add(c);
System.out.println("Certificate found : " + c);
}
ret.add("attestationRootCertificates", certs);
}
List<EcdaaTrustAnchor> ecdaaTrustAnchors = st.getEcdaaTrustAnchors();
if (ecdaaTrustAnchors != null) {
JsonArrayBuilder trustAnchors = Json.createArrayBuilder();
for (EcdaaTrustAnchor t : ecdaaTrustAnchors) {
try {
trustAnchors.add(objectMapper.writeValueAsString(t));
} catch (JsonProcessingException ex) {
Logger.getLogger(MDS.class.getName()).log(Level.SEVERE, null, ex);
}
}
ret.add("ecdaaTrustAnchors", trustAnchors);
}
break;
}
}
if (entry==null && st==null) {
error.add("message", "Could not find metadata for aaguid "+aaguid);
errors.add(error);
}
ret.add("errors", errors);
return ret.build();
}
@Lock(LockType.WRITE)
public void remove(Session session) {
sessions.remove(session);
}