Last active
March 2, 2020 16:58
-
-
Save lightoze/db160266ec4faeaa0688b474ff8d8df4 to your computer and use it in GitHub Desktop.
Micronaut issue 2860
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import io.micronaut.runtime.Micronaut; | |
public class Application { | |
public static void main(String[] args) { | |
Micronaut.run(Application.class); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'java' | |
id 'application' | |
} | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
implementation platform('io.micronaut:micronaut-bom:1.3.2') | |
annotationProcessor platform('io.micronaut:micronaut-bom:1.3.2') | |
annotationProcessor 'io.micronaut:micronaut-inject-java' | |
implementation 'io.micronaut:micronaut-runtime' | |
implementation 'io.micronaut:micronaut-http-server-netty' | |
implementation 'io.micronaut:micronaut-http-client' | |
implementation 'ch.qos.logback:logback-classic:1.2.3' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import io.micronaut.context.annotation.Context; | |
import javax.inject.Singleton; | |
@Singleton | |
@Context | |
public class TestBean { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import io.micronaut.context.event.BeanCreatedEvent; | |
import io.micronaut.context.event.BeanCreatedEventListener; | |
import io.micronaut.core.type.Argument; | |
import io.micronaut.http.HttpRequest; | |
import io.micronaut.http.HttpResponse; | |
import io.micronaut.http.client.BlockingHttpClient; | |
import io.micronaut.http.client.LoadBalancer; | |
import io.micronaut.http.client.RxHttpClient; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.inject.Singleton; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.Map; | |
@Singleton | |
public class TestListener implements BeanCreatedEventListener<TestBean> { | |
private static final Logger log = LoggerFactory.getLogger(TestListener.class); | |
private final URL url = new URL("https://api6.ipify.org"); | |
public TestListener() throws MalformedURLException { | |
} | |
@Override | |
public TestBean onCreated(BeanCreatedEvent<TestBean> event) { | |
log.warn(">>> TestBean created"); | |
// event.getSource().getBean(ObjectMapper.class); | |
LoadBalancer loadBalancer = LoadBalancer.fixed(url); | |
BlockingHttpClient client = event.getSource().createBean(RxHttpClient.class, loadBalancer).toBlocking(); | |
HttpResponse<Map<String, Object>> response = client.exchange(HttpRequest.GET("/?format=json"), Argument.mapOf(String.class, Object.class)); | |
Map<String, Object> body = response.body(); | |
log.warn(">>> Response {}", body); | |
return event.getBean(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment