有时在处理Java流()时,我发现自己需要使用非终端forEach()来触发副作用,但不会终止处理.
我怀疑我可以使用类似.map(item - > f(item))的方法执行此操作,其中方法f执行副作用并将项目返回到流,但它似乎有点笨拙.
有没有一种标准的处理方式?
我有一个AspectJ跟踪例程设置为使用以下切入点记录方法进入和退出条件:
public aspect Trace {
pointcut anyMethodExecuted(): execution (* biz.ianw.lanchecker.*.*(..)) && !within(Trace) && !within( is(AnonymousType) );
pointcut anyConstructorExecuted(): execution (biz.ianw.lanchecker.*.new(..)) && !within(Trace);
Run Code Online (Sandbox Code Playgroud)
在我的sendEmail类中,我有一个方法,它调用setDebugOut方法将调试输出重定向到LogOutputStream:
final private static Logger log = LoggerFactory.getLogger(MailMail.class);
...
LogOutputStream losStdOut = new LogOutputStream() {
@Override
protected void processLine(String line, int level) {
log.debug(line);
}
};
public void sendPlainHtmlMessage(...) {
Session session = javaMailSender.getSession();
PrintStream printStreamLOS = new PrintStream(losStdOut);
session.setDebugOut(printStreamLOS);
...
Run Code Online (Sandbox Code Playgroud)
这很好,除了Trace类切入点截取调用匿名内部类,产生输出:
20:14:18.908 TRACE [biz.ianw.lanchecker.Trace] - Enters method: Logger biz.ianw.lanchecker.MailMail.access$0()
20:14:18.909 TRACE [biz.ianw.lanchecker.Trace] - Exits method: Logger biz.ianw.lanchecker.MailMail.access$0(). …Run Code Online (Sandbox Code Playgroud) 我正试图在Eclipse下开始使用JMH.我可以从命令行构建一个jar来执行,但是我也希望能够在Eclipse中直接运行它以便于开发.
目前我正在:
java.lang.RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList
Run Code Online (Sandbox Code Playgroud)
我正在使用来自http://nitschinger.at/Using-JMH-for-Java-Microbenchmarking/的简单入门案例:
public class MyBenchmark {
@Benchmark
public void testMethod() {
// This is a demo/sample template for building your JMH benchmarks. Edit as needed.
// Put your benchmark code here.
}
public static void main(String... args) throws Exception {
Options opts = new OptionsBuilder()
.include(".*")
.warmupIterations(10)
.measurementIterations(10)
// .jvmArgs("-server")
.forks(1)
// .outputFormat(OutputFormatType.TextReport)
.build();
new Runner(opts).run();
}
}
Run Code Online (Sandbox Code Playgroud)
我按照JMH文档中的规定生成了POM,并添加了JMH中指定的exec-maven-plugin 无法找到资源:/ META-INF/BenchmarkList:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId> …Run Code Online (Sandbox Code Playgroud) 我试图从我的 Maven 构建中排除一个资源文件。我复制了 my-jar-with-dependencies.xml 并添加了一个部分。这正确地排除了命名文件被复制到 jar 的 /src/main/resources但文件仍然与其他资源一起被复制到 jar 的顶级目录。
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<!-- TODO: a jarjar format would be better -->
<id>my-jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory></outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<excludes>
<exclude>**/lanchecker.properties</exclude> <!-- this removes it from jar/src/main/resources but *NOT* from jar/ -->
</excludes>
</fileSet>
</fileSets>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
Run Code Online (Sandbox Code Playgroud)
我可以阻止 maven-assembly-plugin 执行该复制操作,但仍然可以在开发中访问该文件吗?
更新:
依赖项,根据要求:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId> …Run Code Online (Sandbox Code Playgroud) 刚开始在Spring Boot启动时遇到IllegalAccessError.
Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.springframework.core.convert.support.DefaultConversionService.addCollectionConverters(Lorg/springframework/core/convert/converter/ConverterRegistry;)V from class org.springframework.boot.bind.RelaxedConversionService
at org.springframework.boot.bind.RelaxedConversionService.<init>(RelaxedConversionService.java:52)
at org.springframework.boot.bind.RelaxedDataBinder.modifyProperties(RelaxedDataBinder.java:148)
at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:128)
at org.springframework.validation.DataBinder.bind(DataBinder.java:715)
at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:269)
at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:241)
at org.springframework.boot.context.config.ConfigFileApplicationListener.bindToSpringApplication(ConfigFileApplicationListener.java:230)
at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:181)
at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:166)
at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:152)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:119)
at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:65)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:325)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113)
at biz.ianw.LanCheckerReloader.Application.main(Application.java:23)
Run Code Online (Sandbox Code Playgroud)
这似乎与升级到1.3.0.RELEASE时弹簧启动的问题相似,但我没有覆盖我的Spring版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency> …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个简单的实用程序函数来从Map获取值,如果找不到它来创建一个新的值类并将其放在地图中.
看起来虽然很难在运行时获得地图的关键和值的类,但我能想出的最好的东西是以下几行中可怕的东西.
有没有更好的办法?
private Object getOrCreate( Map<Object, Object> map, Object key, Class<?> mapValueClass ) {
Object value = map.get( key );
if (value == null) {
Constructor<?> con = mapValueClass.getConstructor( key.getClass() );
value = con.newInstance( key );
map.put( key, value );
}
return value;
}
Run Code Online (Sandbox Code Playgroud) 我正在研究一个方面方面,需要知道它从哪里调用.目前我正在使用
new Throwable().getStackTrace();
Run Code Online (Sandbox Code Playgroud)
访问此信息,但每个方面需要几百微秒才能运行.
我看过SecurityManager,但似乎只能得到我的类名.
还有其他我错过的选择吗?
更新
JMH基准测试结果在我对@ apangin答案的评论中提到:
Benchmark Mode Cnt Score Error Units
MyBenchmark.javalangaccess13i avgt 100 2025.865 ± 8.133 ns/op
MyBenchmark.javalangaccess2i avgt 100 2648.598 ± 24.369 ns/op
MyBenchmark.throwable1 avgt 100 12706.978 ± 84.651 ns/op
Run Code Online (Sandbox Code Playgroud)
基准代码:
@Benchmark
public StackTraceElement[] throwable1() {
return new Throwable().getStackTrace();
}
@SuppressWarnings("restriction")
@Benchmark
public static StackTraceElement javalangaccess2i() {
Exception e = new Exception();
return sun.misc.SharedSecrets.getJavaLangAccess().getStackTraceElement(e, 2);
}
@SuppressWarnings("restriction")
@Benchmark
public static StackTraceElement javalangaccess13i() {
Exception e = new Exception();
return sun.misc.SharedSecrets.getJavaLangAccess().getStackTraceElement(e, 13);
}
Run Code Online (Sandbox Code Playgroud)
在戴尔XPS13 9343(i5-5200U …
java ×6
aspectj ×2
aop ×1
eclipse ×1
java-stream ×1
jmh ×1
maven ×1
performance ×1
spring ×1
spring-boot ×1
stack-trace ×1