This file contains 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
fun NettyApplicationEngine.addShutdownHook() { | |
Runtime.getRuntime().addShutdownHook(Thread { | |
stop(1, 1, TimeUnit.SECONDS) | |
}) | |
Thread.currentThread().join() | |
} | |
fun Application.addShutdownEvent(connection: CordaRPCConnection) { | |
environment.monitor.subscribe(ApplicationStopped) { | |
connection.notifyServerAndClose() |
This file contains 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
fun Routing.messages(proxy: CordaRPCOps) { | |
route("/messages") { | |
get("/") { | |
call.respond( | |
HttpStatusCode.OK, | |
proxy.vaultQueryBy<MessageState>().states.map { it.state.data }) | |
} | |
post("/") { | |
val received = call.receive<Message>() | |
try { |
This file contains 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
fun ContentNegotiation.Configuration.cordaJackson(proxy: CordaRPCOps) { | |
val mapper: ObjectMapper = JacksonSupport.createDefaultMapper(proxy) | |
mapper.apply { | |
setDefaultPrettyPrinter(DefaultPrettyPrinter().apply { | |
indentArraysWith(DefaultPrettyPrinter.FixedSpaceIndenter.instance) | |
indentObjectsWith(DefaultIndenter(" ", "\n")) | |
}) | |
} | |
val converter = JacksonConverter(mapper) | |
register(ContentType.Application.Json, converter) |
This file contains 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
fun connectToNode( | |
host: String = System.getProperty("config.rpc.host"), | |
rpcPort: Int = System.getProperty("config.rpc.port").toInt(), | |
username: String = System.getProperty("config.rpc.username"), | |
password: String = System.getProperty("config.rpc.password") | |
): CordaRPCConnection { | |
val rpcAddress = NetworkHostAndPort(host, rpcPort) | |
val rpcClient = CordaRPCClient(rpcAddress) | |
return rpcClient.start(username, password) | |
} |
This file contains 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
fun main() { | |
embeddedServer( | |
Netty, | |
port = System.getProperty("server.port").toInt(), | |
module = Application::module | |
).start().addShutdownHook() | |
} | |
fun Application.module() { | |
val connection: CordaRPCConnection = connectToNode() |
This file contains 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
buildscript { | |
ext.ktor_version = '1.2.2' | |
ext.kotlin_version_for_app = '1.3.41' | |
repositories { | |
mavenCentral() | |
jcenter() | |
} | |
dependencies { |
This file contains 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
@StartableByRPC | |
class DoSomethingComplicatedThatYouCantDoViaRpc(private val recipient: Party) : | |
FlowLogic<List<MessageSchema.PersistentMessage>>() { | |
override fun call(): List<MessageSchema.PersistentMessage> { | |
return serviceHub.withEntityManager { | |
createQuery( | |
"SELECT m FROM $TABLE_NAME m WHERE m.recipient = ?1", | |
MessageSchema.PersistentMessage::class.java | |
).setParameter( | |
1, |
This file contains 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
@StartableByRPC | |
class ExecuteSomeInternalNodeLogicYouDontWantToDuplicateFlow(private val recipient: Party) : | |
FlowLogic<List<MessageState>>() { | |
override fun call(): List<MessageState> { | |
return serviceHub.cordaService(MessageRepository::class.java) | |
.findAllMessagesByRecipient(recipient) | |
} | |
} | |
@CordaService |
This file contains 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
@StartableByRPC | |
class GetMeSomeValueFromAService : FlowLogic<Long>() { | |
override fun call(): Long { | |
return serviceHub.cordaService(IncrementingService::class.java).counter | |
} | |
} | |
@CordaService | |
class IncrementingService(serviceHub: AppServiceHub) : SingletonSerializeAsToken() { |
This file contains 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
proxy.startFlow(::StupidSimpleQueryFlow, "asdas").returnValue.get() |
NewerOlder