我看到lambda的类是isSynthetic() && !isLocalOrAnonymousClass(),但我认为代理类可能也是如此.
当然,我可以检查getDeclaredMethods().length == 1并申请regexp课程名称.
但是我想知道是否有一个更优雅和更健壮的选项来查明给定对象是否是lambda.
好吧,我们有FunctionalInterface:
public interface Consumer<T> {
void accept(T t);
}
Run Code Online (Sandbox Code Playgroud)
我可以像以下一样使用它:
.handle(Integer p -> System.out.println(p * 2));
Run Code Online (Sandbox Code Playgroud)
我们如何generic type在代码中解析lambda参数的实际值?
当我们将它用作内联实现时,Integer从该类的方法中提取它并不困难.
我想念什么吗?或者只是java不支持lambda类?
为了更清洁:
那个lambda被包裹着MethodInvoker(在提到中handle),它在其execute(Message<?> message)提取实际参数中进行进一步的反射方法调用.在此之前,它使用Spring将提供的参数转换为目标参数ConversionService.
handle在这种情况下,该方法是在实际应用程序工作之前的一些配置器.
不同的问题,但期望同一问题的解决方案:Java:使用lambda参数获取实际类型的泛型方法
我是Spring的新成员,并在Spring集成http模块中工作,以满足我的项目需求.我从出站网关发送请求作为http客户端.我正在尝试向服务器发起请求,服务器应该使用我的设置值返回消息有效负载.我正在将对象转换为JSON使用发送到服务器我正在从客户端(HttpClientDemo)向服务器端的入站网关发送请求,如下所示.为此,我将我的对象转换为JSON,然后在客户端转换为JSON字符串到对象,在那里执行一些简单的操作并将其发送回客户端(HttpClientDemo)但在此之前,我得到与之相关的异常HttpMessageConverter如下:
Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.mycompany.MyChannel.model.FFSampleResponseHttp] and content type [text/plain;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:784)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:769)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:549)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:517)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:462)
at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:421)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:170)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
at org.springframework.integration.channel.AbstractSubscribablMyChannel.doSend(AbstractSubscribablMyChannel.java:77)
at org.springframework.integration.channel.AbstractMessagMyChannel.send(AbstractMessagMyChannel.java:255)
at org.springframework.integration.channel.AbstractMessagMyChannel.send(AbstractMessagMyChannel.java:223)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:114)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:44)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:93)
Run Code Online (Sandbox Code Playgroud)
请在下面找到相关代码:
客户端代码:HttpClientDemo.java
public class HttpClientDemo {
private static Logger logger = Logger.getLogger(HttpClientDemo.class);
public static void main(String[] args) {
ApplicationContext context = …Run Code Online (Sandbox Code Playgroud) 将项目从 Spring Boot v2.7 迁移到 v3.0(从而从 Spring Integration v5.5 迁移到 v6.0)后,会打印出以下警告:
\nWARN 22084 --- [ restartedMain] ocalVariableTableParameterNameDiscoverer : Using deprecated \'-debug\' fallback for parameter name resolution. Compile the affected code with \'-parameters\' instead or avoid its introspection: com.foobar.MyClassA\nWARN 22084 --- [ restartedMain] ocalVariableTableParameterNameDiscoverer : Using deprecated \'-debug\' fallback for parameter name resolution. Compile the affected code with \'-parameters\' instead or avoid its introspection: com.foobar.MyClassB\nWARN 22084 --- [ restartedMain] ocalVariableTableParameterNameDiscoverer : Using deprecated \'-debug\' fallback for parameter name resolution. Compile …Run Code Online (Sandbox Code Playgroud) 根据这里提供的文档,我正在尝试使用POC将消息传入监听器,如同一文档中所述,下面是我编写配置的方法.
@Configuration
public class KafkaConsumerConfig {
public static final String TEST_TOPIC_ID = "record-stream";
@Value("${kafka.topic:" + TEST_TOPIC_ID + "}")
private String topic;
@Value("${kafka.address:localhost:9092}")
private String brokerAddress;
/*
@Bean public KafkaMessageDrivenChannelAdapter<String, String> adapter(
KafkaMessageListenerContainer<String, String> container) {
KafkaMessageDrivenChannelAdapter<String, String>
kafkaMessageDrivenChannelAdapter = new
KafkaMessageDrivenChannelAdapter<>( container, ListenerMode.record);
kafkaMessageDrivenChannelAdapter.setOutputChannel(received()); return
kafkaMessageDrivenChannelAdapter; }
@Bean public QueueChannel received() { return new QueueChannel(); }
*/
@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setConcurrency(3);
factory.getContainerProperties().setPollTimeout(30000);
return factory;
}
/* …Run Code Online (Sandbox Code Playgroud) Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-07-25 09:17:32.977 ERROR 8176 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Failed to start bean 'subProtocolWebSocketHandler'; nested exception is java.lang.IllegalArgumentException: No handlers
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:184) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:173) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:52) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:157) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:121) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:885) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:161) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] …Run Code Online (Sandbox Code Playgroud) 我很难在Spring AMQP/Rabbit MQ中找到预定/延迟消息的方法.
经过大量的搜索仍然无法在Spring AMQP中做到这一点.有人可以告诉我如何在Spring AMQP中进行x-delay.
如果消费者方面发生了一些异常,我想延迟消息.RabbitMQ说要添加x-delay并安装我已经完成的插件,但仍然会立即收到消息而没有任何延迟
我收到的消息已
收到<(正文:'[B @ 60a4ae5f(byte [26])'MessageProperties [headers = {x-delay = 15000}
@Bean
ConnectionFactory connectionFactory(){
CachingConnectionFactory connectionFactory=new CachingConnectionFactory("127.0.0.1");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setPort(1500);
connectionFactory.setPublisherReturns(true);
return connectionFactory;
}
@Bean
Binding binding(@Qualifier("queue")Queue queue, DirectExchange exchange) {
return new Binding(queue.getName(), Binding.DestinationType.QUEUE, exchange.getName(), queue.getName(), null);
//return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
@Bean
DirectExchange exchange() {
DirectExchange exchange=new DirectExchange("delay-exchange");
return exchange;
}
Run Code Online (Sandbox Code Playgroud)
消费者---
@Override
public void onMessage(Message message, Channel channel) throws Exception {
System.out.println("Received <" + message+ ">" +rabbitTemplate);
if(i==1){
AMQP.BasicProperties.Builder props = …Run Code Online (Sandbox Code Playgroud) 任何人都知道单个监听器是否可以监听下面的多个主题?我知道只有"topic1"有效,如果我想添加其他主题怎么办?你能否在下面展示两个例子?谢谢您的帮助!
@KafkaListener(topics = "topic1,topic2")
public void listen(ConsumerRecord<?, ?> record, Acknowledgment ack) {
System.out.println(record);
}
Run Code Online (Sandbox Code Playgroud)
要么
ContainerProperties containerProps = new ContainerProperties(new TopicPartitionInitialOffset("topic1, topic2", 0));
Run Code Online (Sandbox Code Playgroud) 有人可以告诉我为什么这不起作用:
@Test
public void should_parse_json() {
Expression expression = new SpelExpressionParser().parseExpression("#jsonPath(get('JsonData'), '$.someData')");
Map<String, Object> data = new HashMap<>();
data.put("JsonData", "{\"someData\": 100}");
StandardEvaluationContext context = new StandardEvaluationContext(data);
context.addPropertyAccessor(new JsonPropertyAccessor());
assertThat(expression.getValue(context, Object.class)).isEqualTo(100);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误“org.springframework.expression.spel.SpelEvaluationException:EL1006E:找不到函数‘jsonPath’”
我在类路径中有以下 jar:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
SPEL 文档对我没有帮助。
我正在使用Spring websockets和stomp.js创建一个示例聊天应用程序,我正在使用tomcat 7.54,但在运行应用程序时,我在浏览器发出xhr请求时遇到了异步支持的错误.
服务器信息:Apache Tomcat/7.0.54 Servlet版本:3.0 JSP版本:2.2 Java版本:1.7.0_25
在web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<async-supported>true</async-supported>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
Run Code Online (Sandbox Code Playgroud)
调度员servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<context:component-scan base-package="hello" />
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/hello">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker> …Run Code Online (Sandbox Code Playgroud) spring ×7
java ×4
spring-boot ×3
lambda ×2
apache-kafka ×1
generics ×1
java-8 ×1
jsonpath ×1
rabbitmq ×1
servlets ×1
spring-amqp ×1
spring-el ×1
spring-kafka ×1
spring-mvc ×1
tomcat ×1