Code example for AtomicInteger

Methods: incrementAndGet, set

0
	 
	public int getNextId() { 
		if (!initialised) {
			initialise(); 
		} 
		return messageIdFountain.incrementAndGet();
	} 
	 
	private void initialise() { 
		List<Integer> latestMessageId = getJdbcTemplate().query("select max(id) from Messages", new RowMapper<Integer>(){
 
			public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
				return rs.getInt(1);
			} 
			 
		}); 
		messageIdFountain = new AtomicInteger();
		messageIdFountain.set(latestMessageId.get(0));
	} 
 
	public int getLastId() { 
		return messageIdFountain.get();
	}