javax.ejb.Lock Java Examples

The following examples show how to use javax.ejb.Lock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OrderStore.java    From jaxrs-hypermedia with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: OrderStore.java    From jaxrs-hypermedia with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: OrderStore.java    From jaxrs-hypermedia with Apache License 2.0 5 votes vote down vote up
@Lock(LockType.READ)
public List<Order> getOrders() {
    final ArrayList<Order> orders = new ArrayList<>(this.orders.values());
    orders.sort(Comparator.comparing(Order::getId));

    return orders;
}
 
Example #4
Source File: IndexerClientProducer.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
@Lock(LockType.READ)
@Produces
@ApplicationScoped
public JestClient produce() {
    LOGGER.log(Level.INFO, "Producing ElasticSearch rest client");
    return client;
}
 
Example #5
Source File: ShoppingCart.java    From jaxrs-hypermedia with Apache License 2.0 5 votes vote down vote up
@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++);
    }
}
 
Example #6
Source File: ReadersWritersSingleton.java    From training with MIT License 5 votes vote down vote up
@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());
}
 
Example #7
Source File: ShoppingCart.java    From jaxrs-hypermedia with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #8
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 5 votes vote down vote up
@Lock(WRITE)
public void buyTicket(int seatId) {
    final Seat seat = getSeat(seatId);
    final Seat bookedSeat = seat.getBookedSeat();
    addSeat(bookedSeat);

    seatEvent.fire(bookedSeat);
}
 
Example #9
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 5 votes vote down vote up
@Lock(WRITE)
public void buyTicket(int seatId) {
    final Seat seat = getSeat(seatId);
    final Seat bookedSeat = seat.getBookedSeat();
    addSeat(bookedSeat);

    seatEvent.fire(bookedSeat);
}
 
Example #10
Source File: EventsResource.java    From Architecting-Modern-Java-EE-Applications with MIT License 5 votes vote down vote up
@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);
}
 
Example #11
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 5 votes vote down vote up
@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());
}
 
Example #12
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #13
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #14
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #15
Source File: EventsResource.java    From Architecting-Modern-Java-EE-Applications with MIT License 5 votes vote down vote up
@Lock(WRITE)
public void onEvent(@Observes DomainEvent domainEvent) {
    String message = domainEvent.getContents();
    messages.add(message);

    OutboundSseEvent event = createEvent(message, ++lastEventId);

    sseBroadcaster.broadcast(event);
}
 
Example #16
Source File: ReadersWritersSingleton.java    From training with MIT License 5 votes vote down vote up
@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());
}
 
Example #17
Source File: OrderStore.java    From jaxrs-hypermedia with Apache License 2.0 5 votes vote down vote up
@Lock(LockType.READ)
public List<Order> getOrders() {
    final ArrayList<Order> orders = new ArrayList<>(this.orders.values());
    orders.sort(Comparator.comparing(Order::getId));

    return orders;
}
 
Example #18
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #19
Source File: ShoppingCart.java    From jaxrs-hypermedia with Apache License 2.0 5 votes vote down vote up
@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++);
    }
}
 
Example #20
Source File: ShoppingCart.java    From jaxrs-hypermedia with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: ConfigurationServiceBean.java    From development with Apache License 2.0 5 votes vote down vote up
@Override
@Lock(LockType.WRITE)
public void setConfigurationSetting(String informationId, String value) {
    ConfigurationSetting configSetting = new ConfigurationSetting(
            ConfigurationKey.valueOf(informationId),
            Configuration.GLOBAL_CONTEXT, value);
    setConfigurationSetting(configSetting);
}
 
Example #22
Source File: MDS.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #23
Source File: ConfigurationServiceBean.java    From development with Apache License 2.0 5 votes vote down vote up
@Schedule(minute = "*/10")
@Lock(LockType.WRITE)
public void refreshCache() {
    cache = new HashMap<>();
    for (ConfigurationSetting configurationSetting : getAllConfigurationSettings()) {
        addToCache(configurationSetting);
    }
}
 
Example #24
Source File: OrderStore.java    From jaxrs-hypermedia with Apache License 2.0 5 votes vote down vote up
@Lock(LockType.READ)
public List<Order> getOrders() {
    final ArrayList<Order> orders = new ArrayList<>(this.orders.values());
    orders.sort(Comparator.comparing(Order::getId));

    return orders;
}
 
Example #25
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #26
Source File: ShoppingCart.java    From jaxrs-hypermedia with Apache License 2.0 5 votes vote down vote up
@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++);
    }
}
 
Example #27
Source File: ShoppingCart.java    From jaxrs-hypermedia with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #28
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #29
Source File: SessionRegistry.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 4 votes vote down vote up
@Lock(LockType.WRITE)
public void add(Session session) {
    sessions.add(session);
}
 
Example #30
Source File: TheatreBox.java    From packt-java-ee-7-code-samples with GNU General Public License v2.0 4 votes vote down vote up
@Lock(READ)
public Collection<Seat> getSeats() {
    return Collections.unmodifiableCollection(seats.values());
}