curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install micronaut
mn create-app cz.jopenspace.conference
cd conference
mn create-bean conference-service
mn create-job summary
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Value;
import javax.inject.Named;
import javax.inject.Singleton;
@Singleton
@Requires(env = "attendees.max")
class ConferenceService {
private final VenueService venueService;
private final AttendeeService attendeeService;
private final int maxAttendees;
ConferenceService(
VenueService venueService,
@Named("jOpenSpace") AttendeeService attendeeService,
@Value("attendees.max") int maxAttendees
) {
this.venueService = venueService;
this.attendeeService = attendeeService;
this.maxAttendees = maxAttendees;
}
}
import javax.inject.Singleton;
import io.micronaut.scheduling.annotation.Scheduled;
@Singleton
public class SummaryJob {
@Scheduled(fixedRate = "5m")
public void process() {}
}
$ConferenceServiceDefinition.class
$ConferenceServiceDefinitionClass$$AnnotationMetadata.class
$ConferenceServiceDefinitionClass.class
ConferenceService.class
@Controller("/hello")
public class HelloController {
@Get("/{world}")
public String index(String world) {
return "Hello" + world;
}
}
@Controller("/receive")
public class MessageController {
@Post(value = "/echo", consumes = MediaType.TEXT_PLAIN)
String echo(@Size(max = 1024) @Body String text) {
return text;
}
}
mn create-controller Hello
@Controller("/people")
public class PersonController {
Map<String, Person> inMemoryDatastore = new LinkedHashMap<>();
@Post
public Single<HttpResponse<Person>> save(@Body Single<Person> person) {
return person.map(p -> {
inMemoryDatastore.put(p.getFirstName(), p);
return HttpResponse.created(p);
}
);
}
}
import io.micronaut.context.ApplicationContext
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.*
class HelloControllerSpec extends Specification {
@Shared @AutoCleanup EmbeddedServer embeddedServer =
ApplicationContext.run(EmbeddedServer)
@Shared @AutoCleanup HttpClient client = HttpClient.create(embeddedServer.URL)
void "test hello world response"() {
expect:
client.toBlocking()
.retrieve(HttpRequest.GET('/hello/Mars')) == "Hello Mars"
}
}
class HelloControllerSpec extends Specification {
@Shared @AutoCleanup EmbeddedServer embeddedServer = Application.run(EmbeddedServer)
@Rule Gru gru = Gru.equip(Http.steal(this))
void setup() {
String serverUrl = embeddedServer.getURL().toString()
gru.prepare {
baseUri serverUrl
}
}
void 'test micronaut with Gru'() {
expect:
gru.test {
get '/hello/Mars'
expect {
text inline('Hello Mars!')
}
}
}
}
@Retryable @Client("/pets")
public interface PetClient extends PetOperations {
@Override
Single<Pet> save(String name, int age);
}
@Validated
public interface PetOperations {
@Post
Single<Pet> save(@NotBlank String name, @Min(1L) int age);
}
@Inject @Field ConferenceService conferenceService
@CompileStatic
String conference() {
"conference"
}
mn create-function conference --lang groovy
@FunctionClient
interface ConferenceClient {
@Named("conference")
Single<String> conference()
}
class ConferenceFunctionSpec extends Specification {
void "test conference function"() {
given:
EmbeddedServer server = ApplicationContext.run(EmbeddedServer)
ConferenceClient client = server.getApplicationContext().getBean(ConferenceClient)
expect:
client.conference().blockingGet() == "conference"
cleanup:
if(server != null) server.stop()
}
}
cd conference
./gradlew deploy
14:00 v salónku