Contenu connexe Similaire à システムのモダナイズ 落ちても良いアプリの作り方 (20) システムのモダナイズ 落ちても良いアプリの作り方2. CONFIDENTIAL Designator
Source:
Insert source data here
Insert source data here
Insert source data here
OPTIONAL SECTION MARKER OR TITLE
伊藤ちひろ (Chihiro Ito)
OpenJDK Committer
Middleware Solution Architect
Twitter : @chiroito
自己紹介
13. CONFIDENTIAL Designator
サンプルソース
Java EESpring Boot
@SessionScope
@Component
public class Counter implements Serializable {
}
@RestController
@RequestMapping("/session")
public class CountEndpoint {
private final Counter counter;
public CountEndpoint(Counter counter) {
this.counter = counter;
}
}
@SessionScoped
public class Counter implements Serializable {
}
@Path("/session")
public class CountEndpoint {
@Inject
private Counter counter;
}
36. CONFIDENTIAL Designator
Java EEのサンプルソース
CountServlet.javaCounter.java
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
@SessionScoped
public class Counter implements Serializable {
private static final long serialVersionUID = 1L;
private int value;
public int getValue() {
return value;
}
public void countUp() {
this.value++;
}
}
@WebServlet("/count")
public class CountServlet extends HttpServlet {
@Inject
Counter counter;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse
res) throws ServletException {
counter.countUp();
res.setContentType("text/html");
try(PrintWriter writer = res.getWriter()) {
writer.println("<html><body>");
writer.println("<h1> counter=" + counter.getValue() + "</h1>");
writer.println("</body></html>");
}catch(IOException e){
throw new ServletException(e);
}
}
}
37. CONFIDENTIAL Designator
Java EEのサンプルソース
CountEndpoint.javaMyApplication.java
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/rest")
public class MyApplication extends Application {
}
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/session")
public class CountEndpoint {
@Inject
private Counter counter;
@GET
@Path("count")
public String getClichedMessage() {
counter.countUp();
return "counter=" + counter.getValue();
}
}
38. CONFIDENTIAL Designator
Java EEのサンプルソース
jboss-web.xmlweb.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<distributable/>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss-web_19_0.xsd"
version="10.0">
<replication-config>
<cache-name>web.baz</cache-name>
</replication-config>
</jboss-web>
39. CONFIDENTIAL Designator
Wildflyの環境設定
HAモードのWildflyがInfinispanへセッションを格納するように設定
Wildfly の解凍先で以下のように jboss-cli を実行
binjboss-cli.bat
connect
batch
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=infinispan-server1:add(host=127.0.0.1, port=11222)
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=infinispan-server2:add(host=127.0.0.1, port=11322)
run-batch
batch
/subsystem=infinispan/remote-cache-container=foo:add(default-remote-cluster=bar)
/subsystem=infinispan/remote-cache-container=foo/remote-cluster=bar:add(socket-bindings=[infinispan-server1, infinispan-server2])
run-batch
batch
/subsystem=infinispan/cache-container=web/invalidation-cache=baz:add()
/subsystem=infinispan/cache-container=web/invalidation-cache=baz/component=transaction:add(mode=BATCH)
/subsystem=infinispan/cache-container=web/invalidation-cache=baz/component=locking:add(isolation=REPEATABLE_READ)
/subsystem=infinispan/cache-container=web/invalidation-cache=baz/store=hotrod:add(remote-cache-container=foo, fetch-state=false,
purge=false, passivation=false, shared=true)
run-batch
42. CONFIDENTIAL Designator
SpringBootのサンプルソース
CountEndpoint.javaCounter.java
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.SessionScope;
import java.io.Serializable;
@SessionScope
@Component
public class Counter implements Serializable {
private static final long serialVersionUID = 1L;
private int value;
public int getValue() {
return value;
}
public void countUp(){
this.value++;
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/session")
public class CountEndpoint {
private final Counter counter;
public CountEndpoint(Counter counter) {
this.counter = counter;
}
@GetMapping("/count")
public String count(){
counter.countUp();
return "counter=" + counter.getValue();
}
}