spring boot: 2.0.3.RELEASE
spring-kafka: 2.1.4.RELEASE
jdk: 1.8
配置文件示例代码
spring:
kafka:
topics: xxx
java监听器示例代码
@KafkaListener(topics = "#{'${spring.kafka.topics}'.split(',')}")
public void receive(@Payload List<String> message) {
message.stream().filter(m -> MessageConverter.Instance.convert(m) != null)
.map((String s) -> MessageConverter.Instance.convert(s))
.forEach(m -> {
//business logic
});
}
spring boot: 2.0.3.RELEASE
jdk: 1.8
1.分环境配置日志
在不同的配置文件定义日志级别,如logging.level.root=debug/info
定义logback-spring.xml文件
<springProperty scope="context" name="logLevel" source="logging.level.root"/>
然后可以引用${logLevel}
2.yml里面配置list,使用@Value取值
mm:
list: aaa,bbb,ccc
@Value("#{'${mm.list}'.split(',')}")
private List<String> lists;
3.yml里面配置map,使用@Value取值
mm:
map: "{'aaa':1, 'bbb':2, 'ccc': 3}"
@Value(#{${"mm.map"}})
private Map<String, Integer> maps;
拿List举例子, 一般理解是说当在这个list上通过iterator遍历, 然后在对list进行更新操作会发生. 这种理解并不准确.
举个例子.
一个线程通过iterator进行遍历, 然后线程T2对list[0]进行读和写. 这样其实不会发生异常.
所以确切一点的说法应该是当进行遍历的时候, 又对list进行了结构性的变更, 才会出现这个异常.
protected transient int modCount = 0;
private class Itr implements Iterator<E> {
//省略
}
int expectedModCount = modCount;
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
jdk1.8 ArrayList源码
MySQL5.7
information schema的一些表说明记录
在innodb存储引擎,包含了当前正在执行的事物
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update blog set amt=111 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
看结果
mysql> select * from innodb_trx \G;
*************************** 1. row ***************************
trx_id: 17263
trx_state: RUNNING
trx_started: 2018-01-22 14:26:00
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 3
trx_mysql_thread_id: 11
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 1
trx_lock_structs: 2
trx_lock_memory_bytes: 1136
trx_rows_locked: 1
trx_rows_modified: 1
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 0
trx_is_read_only: 0
trx_autocommit_non_locking: 0
1 row in set (0.00 sec)
记录了一个事物正在等待另一个事物的记录
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update blog set amt=333 where id=1;
看innodb_trx的结果.
mysql> select * from innodb_trx \G;
*************************** 1. row ***************************
trx_id: 17264
trx_state: LOCK WAIT
trx_started: 2018-01-22 14:27:45
trx_requested_lock_id: 17264:154:3:2
trx_wait_started: 2018-01-22 14:27:45
trx_weight: 2
trx_mysql_thread_id: 12
trx_query: update blog set amt=333 where id=1
trx_operation_state: starting index read
trx_tables_in_use: 1
trx_tables_locked: 1
trx_lock_structs: 2
trx_lock_memory_bytes: 1136
trx_rows_locked: 1
trx_rows_modified: 0
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 0
trx_is_read_only: 0
trx_autocommit_non_locking: 0
*************************** 2. row ***************************
trx_id: 17263
trx_state: RUNNING
trx_started: 2018-01-22 14:26:00
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 3
trx_mysql_thread_id: 11
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 1
trx_lock_structs: 2
trx_lock_memory_bytes: 1136
trx_rows_locked: 1
trx_rows_modified: 1
trx_concurrency_tickets: 0
trx_isolation_level: REPEATABLE READ
trx_unique_checks: 1
trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 0
trx_is_read_only: 0
trx_autocommit_non_locking: 0
2 rows in set (0.00 sec)
看到有2条记录, 第2条记录(trx_id: 17263)和第一部分的结果一致, 那么第1条记录(trx_id: 17264)就是这次sessionB执行的update语句.而且状态是lock wait.
然后看innodb_locks的结果. 可以看到锁模式(X),锁类型(record). lock_trx_id就是上面表的记录id.
mysql> select * from innodb_locks \G;
*************************** 1. row ***************************
lock_id: 17264:154:3:2
lock_trx_id: 17264
lock_mode: X
lock_type: RECORD
lock_table: `jimmy_slave`.`blog`
lock_index: PRIMARY
lock_space: 154
lock_page: 3
lock_rec: 2
lock_data: 1
*************************** 2. row ***************************
lock_id: 17263:154:3:2
lock_trx_id: 17263
lock_mode: X
lock_type: RECORD
lock_table: `jimmy_slave`.`blog`
lock_index: PRIMARY
lock_space: 154
lock_page: 3
lock_rec: 2
lock_data: 1
2 rows in set, 1 warning (0.00 sec)
这个表记录的是哪个事物在等哪个锁.
看下面的数据.
mysql> select * from INNODB_LOCK_WAITS \G;
*************************** 1. row ***************************
requesting_trx_id: 17264
requested_lock_id: 17264:154:3:2
blocking_trx_id: 17263
blocking_lock_id: 17263:154:3:2
1 row in set, 1 warning (0.00 sec)
意思就是说事物(17264)在等待事物(17263)上的锁.
Object obj = new Object();
public void sync() {
synchronized(obj) {
try {
TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("method sync quit");
}
如何唤醒调用的该方法的线程,让方法继续往下执行
public void notifySync() {
obj.notifyAll();
System.out.println("notifySync invoked");
}
实验一下, 第二个线程调用上面的方法, 果然抛了异常:java.lang.IllegalMonitorStateException.
来看一下api对这个异常的说明.
Thrown to indicate that a thread has attempted to wait on an object’s monitor or to notify other threads waiting on an object’s monitor without owning the specified monitor.
也就是说调用wait/notify/notifyAll方法, 那么执行相应方法的线程必须持有监视器才行.