InputStream在构造函数中打开一个然后将其传递给超级构造函数时,有没有什么好的方法可以使用try-with-resources ?
基本上我想要做的是:
public class A {
public A(InputStream stream) {
// Do something with the stream but don't close it since we didn't open it
}
}
public class B {
public B(File file) {
// We open the stream so we need to ensure it's properly closed
try (FileInputStream stream = new FileInputStream(file)) {
super(new FileInputStream(file));
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当然,因为super必须是构造函数中的第一个语句,所以这是不允许的.有没有什么好办法实现这个目标?
在我的应用程序中,我有很多REST服务.我已经为所有服务编写了测试:
org.springframework.web.client.RestTemplate
Run Code Online (Sandbox Code Playgroud)
REST服务调用例如下所示:
final String loginResponse = restTemplate.exchange("http://localhost:8080/api/v1/xy", HttpMethod.POST, httpEntity, String.class)
.getBody();
Run Code Online (Sandbox Code Playgroud)
然后我检查响应体 - 一切正常.缺点是必须启动应用程序才能调用REST服务.
我现在的问题是如何在JUnit- @Test方法中做到这一点?它是一个Spring Boot应用程序(带有嵌入式tomcat).
感谢帮助!
我正在尝试用Python制作一个简单的解码器环.
例:
a=b, `b=c, c=d, etc.
Run Code Online (Sandbox Code Playgroud)
我希望脚本采用编码消息并输出解码的消息.
例如,我会输入"ifmmp"并输出"hello".
我一直在想我需要将所有角色分开并循环遍历它们并改变它们chr()或ord()值.
在python中似乎没有任何关于此的文档.
我们正在为所有项目使用Gradle Wrapper,但我们的一个工具(IntelliJ IDEA)经常重新运行包装器任务,该任务会在每次运行时更改第一行 - 关于文件生成时间的注释.这种变化倾向于被添加到拉取请求中,这意味着存在不必要的合并冲突的高可能性.
正确的解决方案是让所有开发人员永远不会检查文件,除非它实际上以有意义的方式进行了更改,但开发人员(包括我)可能会仓促而健忘,因此文件会在拉取请求中定期显示.
我有一些想法可以解决这个问题,但不能真正决定它们中的任何一个.
让Git忽略像这样的第一行gradle-wrapper.properties,感觉很麻烦,并且必须由每个开发人员完成,因为它是本地的.
在gradle包装器任务中添加一个尾部,删除第一行gradle-wrapper.properties,感觉有点hackish.
不知何故,每当我们同步项目时,IDEA都不会自动重新运行包装器任务,不知道如何执行此操作.
我现在倾向于2.但我想知道是否有其他人有更好的想法.
通常,文件的物理大小大于逻辑大小.我想知道是否有任何相反的情况.可能存在某些文件,其物理大小将小于逻辑大小.
这是我的设置:
第一个服务(FlightIntegrationApplication),它使用FeignClients API和Eureka调用第二个服务(BaggageServiceApplication).
github项目:https://github.com/IdanFridman/BootNetflixExample
第一次服务:
@SpringBootApplication
@EnableCircuitBreaker
@EnableDiscoveryClient
@ComponentScan("com.bootnetflix")
public class FlightIntegrationApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(FlightIntegrationApplication.class).run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
在其中一个控制器中:
@RequestMapping("/flights/baggage/list/{id}")
public String getBaggageListByFlightId(@PathVariable("id") String id) {
return flightIntegrationService.getBaggageListById(id);
}
Run Code Online (Sandbox Code Playgroud)
FlightIntegrationService:
public String getBaggageListById(String id) {
URI uri = registryService.getServiceUrl("baggage-service", "http://localhost:8081/baggage-service");
String url = uri.toString() + "/baggage/list/" + id;
LOG.info("GetBaggageList from URL: {}", url);
ResponseEntity<String> resultStr = restTemplate.getForEntity(url, String.class);
LOG.info("GetProduct http-status: {}", resultStr.getStatusCode());
LOG.info("GetProduct body: {}", resultStr.getBody());
return resultStr.getBody();
}
Run Code Online (Sandbox Code Playgroud)
RegistryService:
@Named
public …Run Code Online (Sandbox Code Playgroud) spring spring-boot spring-cloud netflix-feign netflix-eureka
我写了一个冒泡排序的实现,与Groovy一起玩,看看是否--indy对性能有任何明显的影响.
从本质上讲,它对一千个随机整数的列表进行了一千次排序,并测量了对列表进行排序的平均执行时间.
列表Integer[]的一半是一个,另一半是一个ArrayList<Integer>.
结果让我很困惑:
$ groovyc BubbleSort.groovy
$ time java -cp ~/.gvm/groovy/current/embeddable/groovy-all-2.4.3.jar:. BubbleSort
Average: 22.926ms
Min: 11.202ms
[...] 26.48s user 0.84s system 109% cpu 25.033 total
$ groovyc --indy BubbleSort.groovy
$ time java -cp ~/.gvm/groovy/current/embeddable/groovy-all-2.4.3-indy.jar:. BubbleSort
Average: 119.766ms
Min: 68.251ms
[...] 166.05s user 1.52s system 135% cpu 2:03.82 total
Run Code Online (Sandbox Code Playgroud)
查看基准测试运行时的CPU使用情况,编译时的CPU使用率--indy要比非编译时高很多.

这引起了我的兴趣所以我再次运行基准测试 - 但这次启用了Yourkit代理和CPU跟踪.以下是录制的呼叫树:
没有--indy:



groovy
jruby
invokedynamic
java-8
我正在将我的系统从java迁移到Scala.我在我的java代码中使用了注册表模式来从字符串中获取实现.scala有什么类似的事情吗?我是scala的新手,有人可以指点我正确的参考吗?
我的java代码:
public class ItemRegistry {
private final Map<String, ItemFactory> factoryRegistry;
public ItemRegistry() {
this.factoryRegistry = new HashMap<>();
}
public ItemRegistry(List<ItemFactory> factories) {
factoryRegistry = new HashMap<>();
for (ItemFactory factory : factories) {
registerFactory(factory);
}
}
public void registerFactory(ItemFactory factory) {
Set<String> aliases = factory.getRegisteredItems();
for (String alias : aliases) {
factoryRegistry.put(alias, factory);
}
}
public Item newInstance(String itemName) throws ItemException {
ItemFactory factory = factoryRegistry.get(itemName);
if (factory == null) {
throw new ItemException("Unable to find factory containing alias " …Run Code Online (Sandbox Code Playgroud) 默认情况下,Spring 会修剪用作路径变量的字符串中的前导/尾随空格。我发现这是因为AntPathMatcher中的trimTokens标志默认设置为true。
但我不知道如何将该标志设置为false。
使用AntPathMatcher提供我自己的RequestMappingHandlerMapping bean ,并将其设置为false不起作用。
如何使用 JavaConfig 更改此标志?
谢谢。
我为我的线程池添加了三个具有不同任务和不同延迟的线程,一切正常.
static ScheduledExecutorService scheduleTaskExecutor;
static ScheduledFuture<?> future1;
static ScheduledFuture<?> future2;
static ScheduledFuture<?> future3;
public void onCreate(Bundle savedInstanceState) {
scheduleTaskExecutor = Executors.newScheduledThreadPool(3);
future1 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable1() {...}, 0, olddelay1, TimeUnit.SECONDS); // Task 1
future2 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable2() {...}, 0, olddelay2, TimeUnit.SECONDS); // Task 2
future3 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable3() {...}, 0, olddelay3, TimeUnit.SECONDS); // Task 3
}
Run Code Online (Sandbox Code Playgroud)
稍后在运行时我希望更改其中一个线程的延迟(无论哪个),其他线程将继续使用旧的延迟.我认为ScheduledFuture引用可以帮助并尝试以下代码(例如第二个线程),但是在执行之后,线程池中只剩下一个线程(任务2).
public void changeDelay2(int newdelay2){
future2.cancel(true);
future2 = scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable2() {...}, 0, newdelay2, TimeUnit.SECONDS); // Task 2
}
Run Code Online (Sandbox Code Playgroud)
那么是否有可能只改变一个线程的延迟?
java multithreading delay threadpool scheduledexecutorservice
java ×5
java-8 ×2
spring ×2
spring-boot ×2
decoder ×1
delay ×1
file ×1
filesystems ×1
git ×1
gradle ×1
groovy ×1
jruby ×1
junit ×1
linux ×1
python ×1
rest ×1
scala ×1
spring-cloud ×1
spring-mvc ×1
string ×1
threadpool ×1
unix ×1