ã¨ããã¿ã¤ãã«ã§ã12/13ï¼éï¼ã«éå¬ãããDisruptors Tech Meetã«ç»å£ãã¾ããã
ããã«ã¡ã¯ãContractSã®åéã§ãã
Disruptors Tech Meetã¨ã¯
æ ªå¼ä¼ç¤¾ãã£ã¹ã©ãã¿ã¼ãºã®ã°ã«ã¼ãä¼ç¤¾ã«æå±ããã¨ã³ã¸ãã¢ã
- å社äºä¾ãè¦ã¦ã»èãã¦åºæ¿ãããã
- æ®æ®µè§¦ããªãæè¡ã®è©±ãèãã¦ã¹ãã«ã¢ããã«ç¹ãã
- çºè¡¨ãæè¦äº¤æã®çµé¨ãç©ã
ã¨ãã£ããã¨ãç®çã«ãæ¥é ã®ææãçºè¡¨ã»å
±æããåå¼·ä¼ã§ãã
ContractSã¯ãã£ã¹ã©ãã¿ã¼ãºï¼ãã£ãªã¢ã¤ã³ããã¯ã¹ããåå·å¤æ´ï¼ã°ã«ã¼ãã®ä¸å¡ã¨ãã¦ãç¶ç¶çãªã¢ã¦ããããã®å ´ã¨ãã¦æ´»ç¨ãã¦ãã¾ãã
åä¼ã«ã¯ããã£ãªã¢ã¤ã³ããã¯ã¹ç¤¾ããã¼ã¸ãã«ç¤¾ã®ã¨ã³ã¸ãã¢ãåå ãã¦ãããæ¯åº¦ã¯ã¤ã¯ã¤ã¨æ¥½ããã§ãã¾ãã
Disruptors Tech Meetã¯ãã°ã«ã¼ãä¼ç¤¾éã®ã¯ãã¼ãºããªåå¼·ä¼ã§ã¯ãããã®ã®ãã¯ãªã¼ã¿ã¼æ¯ã«ç¶ç¶éå¬ããããããã3å¹´ç®ã«çªå
¥ãã¾ãã
ããããããªãã¯ãªåå¼·ä¼ã«ãã¦ããçããããã¾ããã楽ãã¿ã«ã
ï¼ä»åã¯ç»å£å´ã§ããããçè
ã¯æ®æ®µã¯éå¶å´ã®äººéã§ãï¼
Springã«ãããã¢ããªã±ã¼ã·ã§ã³ã¤ãã³ã
ç»å£è³æã§ã¯å
·ä½çãªæè¡ã¹ã¿ãã¯ã«è§¦ãã¦ããã¾ããããJava/Spring Bootç°å¢ã§åºç¤ã¯æ§ç¯ãã¦ãã¾ãã
Springã§ã¯ãã¢ããªã±ã¼ã·ã§ã³ã¤ãã³ãã®çºè¡ã¯ApplicationEventPublisher
ã§è¡ãã¾ãã
ApplicationEventPublisher (Spring Framework API) - Javadoc
ä½è«ã§ãããæ°å¹´åã¾ã§ï¼Spring 4.2以åï¼ã¯ã¤ãã³ãçºè¡ã®ããã«ApplicationEvent
ï¼Javadocï¼ã¨ããã¯ã©ã¹ãç¶æ¿ããå¿
è¦ãããã¾ããããä»ç¾å¨ã¯POJOã®ãã¼ã¿ã¢ãã«ã使ããã®ã§ãä»åã®éç¥åºç¤ã ã£ããããã¡ã¤ã³ã¤ãã³ãã¸ã®é©ç¨ã ã£ãããæ´»ç¨ã¯ããªããããããªã£ãã¨æãã¦ãã¾ãã
çºè¡ãããã¤ãã³ãã¯ãTransactionalEventListener
ã¢ããã¼ã·ã§ã³ãä»ä¸ããã¡ã½ããã§ãªãã¹ã³ãã¾ããè©ä¾¡ãããã¿ã¤ãã³ã°ã¯ããã©ã«ãã¯ãã©ã³ã¶ã¯ã·ã§ã³ã³ãããå¾ï¼AFTER_COMMIT
ï¼ã§ãã
TransactionalEventListener (Spring Framework API) - Javadoc
ãµã³ãã«
以ä¸ãç°¡åãªã³ã¼ãã¤ã¡ã¼ã¸ã§ãï¼ã¤ã¡ã¼ã¸ããããåªå ã§ãé©åãªè²¬ååå²ã§ã¯ãªãã§ãâ¦ï¼ã
// ã¤ãã³ãçºè¡å´ public class ApplicationEventNotifier { // éç¥ç¨æé¢ãã¡ã¯ã㪠private final NotifiableContentsFactory notifiableContentsFactory; private final ApplicationEventPublisher applicationEventPublisher; // ã¿ã¹ã¯ãä½æããããã¨ãã¦ã¼ã¶ã¼ã«éç¥ãã public void taskCreated(Set<UserId> destinations) { destinations.stream() .map(userRepository::findById) .map(notifiableContentsFactory::taskCreated) .forEach(this::notify); } private void notify(Notifiable event) { applicationEventPublisher.publishEvent(event); } } // ã¿ã¹ã¯ä½æã¤ãã³ã public record TaskCreated(MailAddress destination, String subject, String body) implements Notifiable { } // ã¤ãã³ãè³¼èªå´ public class NotifiableEventListener { private final MailClient mailClient; // ã¿ã¹ã¯ä½ææã«å¼ã°ãããªã¹ãã¼ @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void onTaskCreated(TaskCreated event) { mailClient.send(event.destination(), event.subject(), event.body()); } }
ãã®ãµã³ãã«ã ãè¦ãã¨å¹æã¯æãã«ããããããã¾ããããå®éã«ã¯éç¥ã¤ãã³ãã¤ã³ã¿ãã§ã¼ã¹ã®çµ±åããªã¹ãã¼ã®å
±éåã«ãã£ã¦ããã¬ã¼ã ã¯ã¼ã¯ã®ãããªå½¢ã§ä½¿ããããã«ãã¦ãã¾ãã
ä»åã¯ã¢ããªã±ã¼ã·ã§ã³ã®æ§é ãæ´çããã®ã¿ã§ãã¡ãã»ã¼ã¸ããã¼ã«ã¼ã®å°å
¥ãªã©ã¾ã§ã¯è¸ã¿è¾¼ãã§ãã¾ãããããããã®ããããæ¡å¼µå¾ãè¨äºã«ã¾ã¨ãã¦ãããã¨æãã¾ãã
æå¾ã«
ç°¡åã§ã¯ããã¾ãããç»å£è³æã®å ¬éã¨ãSpringã«ãããã¢ããªã±ã¼ã·ã§ã³ã¤ãã³ãã®å®è£ ãµã³ãã«ãã¾ã¨ãã¾ãããä»åã®æèã§ã¯éç¥ã®ã¿ã§ãããã¾ãå¥ã®å½¢ã§ãã¡ã¤ã³ã¤ãã³ãã¸é©ç¨ãé²ãã¦ãã¾ãããã¡ããæ©ä¼ãè¦ã¤ãã¦ã¾ã¨ãã¦ãããã¨æãã¾ãã
æå¾ã«ãContractSã§ã¯ä¸ç·ã«ãããã¯ããé²åããã¦ããã¨ã³ã¸ãã¢ãåéä¸ã§ããå°ãã§ãèå³ãããæ¹ããCLMï¼ãªã«ããç¾å³ããã®ï¼ãã¨ããæ¹ãã«ã¸ã¥ã¢ã«é¢è«ããå¯è½ã§ãã®ã§ã以ä¸ã®ãã¼ã¸ãããæ°è»½ã«ã声æããã ããã