0511零散问题整理

1. consumer.assign() vs. consumer.subscribe()

assign是给一个consumer手动分几个partitions,一般也不会触发rebalance;而subscribe是给一个group里的所有consumer动态分配不同topics里的partitions,而且每个consumer必然只会分到一个partition。

再说一点我的认知,assign由于是手动分配partition,经常与seek一起用,再结合poll(),可以查询到任意存在的offset的ConsumerRecord,而我用subscribe的时候,再用poll(),是poll不到任何消息的。推测subscribe更像是订阅消息的更新,而不是查询历史消息。

这里也有个很好的链接,可以初步学习一下consumer

2. Warning equals/hashCode on @Data annotation lombok with inheritance

Add:

@EqualsAndHashCode(callSuper=false) // or true

By setting callSuper to true, you can include the equals and hashCode methods of your superclass in the generated methods.

3. 一个类如果没有抽象方法,可不可以定义为抽象类?如果可以,有什么意义?

A:可以。
B:不让创建对象。

4. Spring中注入/调用其它类的

  • List item

方法的几种方式

  1. 直接用new创建实例
Utils utils = new Utils();
utils.callfunction();
  1. define static method callfunction();
    then directly Utils.callfunction();
public class Utils {
	public static void callfunction() {}
}
Utils.callfunction();
  1. @Component 用在Utils上,代替了1中的new一个instance
    然后直接@Inject utils
@Inject
Utils utils;
utils.callfunction();

5. consumer.poll()一次会遍历多少offset?seek,poll,commit对position的影响?

  1. 之前提到过max-poll-records这个config参数,但是这个应该是只是设置了一个上限,而不是说每次能控制正好遍历max-poll-records个offset,或者返回正好max-poll-records条消息。

  2. The default setting (-1) sets no upper bound on the number of records, i.e. Consumer.poll() will return as soon as either any data is available or the passed timeout expires.

  3. 如果没有poll到消息,那么commitSync()后,offset应该是不会变的,也就是consumer.position()是不会变的。如果poll到了record,再commitSync(),就会更新上最后一个record的offset:record.offset()到当前的位置,consumer.position()也就会有所改变。

  4. consumer.seek(): overrides the fetch offsets the consumer will use on the next poll()。也就是说seek里传入的offset,会覆盖之前commitSync()的offset,当前的consumer.position()自然也会改变了,如下一点(第5点)所说。

  5. 仿佛seek自带commit功能一样,seek后,consumer.position会改变。但是,poll需要配合consumer.commitSync() 去commit offset,才会改变当前的consumer.position()。

  6. 没有试过poll成功后不 commit的话,consumer.position是什么,但是正在poll的时候position应该是+1递增的(poll()的时候是sequetially的),但当第二次poll时,如果上次没有commitSync(),position又会变回为第一次poll的起始位置。

    currentPosition = consumer.position()
    consumer.poll()
    assert(currentPosition == consumer.position())
    

    发现poll是会改变position的,即使还没有commit。

6. SpringJUnit4ClassRunner和SpringRunner有什么不同?

There is no difference, from the javadoc:
SpringRunner is an alias for the SpringJUnit4ClassRunner.

7.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

确保@SpringBootTest(classes=)提到的class是有@SpringBootApplication标注的,我写错@SpringBootTest 要扫描的那些class了。

8. RestTemplate好像需要absolute url,而用TestRestTemplate.exchange() 好像就不用绝对路径而是相对路径

如果用restTemplate.getForEntity():
IllegalArgumentException: URI is not absolute

9. Mockito: unfinishedStubbingException: cannot evaluate … toString()

@Test
public myTest(){
   List<SomeModel> someModelList = getSomeList();
   Mockito.when(mainModel.getList()).thenReturn(someModelList);
}

注意someModelList是需要实例化的,也可以用@InjectMocks去实例化,具体这些区别可以见我这篇博客。

但是有人建议,能new instance(),就最好不要用@InjectMocks。use new instance() in @Before is better than @InjectMocks

@InjectMocks is to inject a mocked object into some object under test.

Use @InjectMocks to create class instances which needs to be tested in test class.

10.BeanTester().testBean(MyBean.class): Testing getter and setters methods

Testing the getter and setter method pairs of a JavaBean can become very tedious. Fortunately there is a nice test library called meanBean that can do the work for us. So after adding the following method to our unit test, we’re finished testing the getters and setters:

Testing the getters and setters of MyBean:

@Test
public void getterAndSetterCorrectness() throws Exception {
    new BeanTester().testBean(MyBean.class);
}

11. s1.split("\\.")

java split(regex)的一些例子

string = "1&area_id=54&cid=3";
string.split("&\\w+=");
// get: [1, 54, 3]

The call to string.split("&\w+=") reads in English: Split string on every match for the regular expression parameter, and then return all substrings in between the matched tokens as an array.

12. Jackson:将json string变为map

 ObjectMapper mapper = new ObjectMapper();
 String json = "{\"name\":\"mkyong\", \"age\":\"37\"}";
 Object map = mapper.readValue(json, Map.class);

13. Gradle set proxy

我遇到的问题:
IDEA,非Android的gradle项目,用的是gradle wrapper。
解决我的方法:

  1. disable offline mode
  2. 在setting的http proxy里选择 no proxy
  3. 然后检查gradle wrapper的properties文件里路径是否正确,是否能下载到gradle.zip文件。

网上还提供了一些其它方法:
File>Invalidate caches/restart>Invalidate and restart.

14. 子类没有覆盖父类同名称的成员变量?

// debug时发现子类变量为:
string1;
string2;
parent.string1;
parent.string2;

JAVA本身并不提供子类“覆盖”父类成员变量的方法,而事实上,从面相对象的角度上来说,子类也不应当可以“覆盖”父类的成员变量。

15. 相比较直接new instance,注入(@Inject/@Autowired)有什么好处?

Interface B {
	//some methods
}
class X implements B{
	// implement some methods of B
}

class Y implements B{
// implement some methods of B
}
// code without using Dependency Injection
class A1{
	private B objB = New X();
	//remaining code
}

class A2{
	private B objB = New X();
	//remaining code
}

如果需要把

private B objB = New X();

改为

private B objB = New Y();

那我们需要改A1和A2。

但如果我们用注入:

// code using Dependency Injection
class A1{
	@Autowired
	private B objB;
	//remaining code
}

class A2{
	@Autowired
	private B objB;
	//remaining code
}

Here you just need to change configuration of creating instances for interface B and change required class from X to Y that’s it. No changes in any of java classes (here A1 and A2).

16. 用RestTemplate或者TestRestTemplate是想要测试什么?

测试一个web application能否正常地make web request.

We can use RestTemplate to test HTTP based restful web services, it doesn’t support HTTPS protocol. RestTemplate class provides overloaded methods for different HTTP methods, such as GET, POST, PUT, DELETE etc.

TestRestTemplate is not an extension of RestTemplate, but rather an alternative that simplifies integration testing and facilitates authentication during tests.

TestRestTemplate: Convenient subclass of {@link RestTemplate} that is suitable for integration tests.

你可能感兴趣的:(0511零散问题整理)