• Nenhum resultado encontrado

Trilha Java O que você precisa saber sobre serviços de mensageria em Java Renan Roggia e Willian Alves

N/A
N/A
Protected

Academic year: 2021

Share "Trilha Java O que você precisa saber sobre serviços de mensageria em Java Renan Roggia e Willian Alves"

Copied!
28
0
0

Texto

(1)

Globalcode – Open4education

Trilha – Java

O que você precisa saber sobre serviços de mensageria em Java Renan Roggia e Willian Alves

(2)

App 1 App 2 App 6 App 5 App 3 App 4 Serviço de mensageria

(3)

Globalcode – Open4education App 1 App 2 App 6 App 5 App 3 App 4

(4)

Agenda

Serviços de mensageria

O que são?

Para que servem?

Vantagens

Protocolos e APIs

Taxonomia de um serviço de mensageria

Demos e Exchanges

(5)

Globalcode – Open4education

O que são?

Serviços independentes orientados a mensagens

Message-Oriented Middleware (MOM)

Messaging System

Messaging Service

(6)

Para que servem?

Troca de informações entre aplicações

Assíncrono

(7)

Globalcode – Open4education

Vantagens

Assíncrono (Fire and Forget)

Desacoplamento entre aplicações

Resiliência

(8)

Vantagens

Assíncrono (Fire and Forget)

Desacoplamento entre aplicações

Resiliência

(9)

Globalcode – Open4education

Vantagens

Assíncrono (Fire and Forget)

Desacoplamento entre aplicações

Resiliência

(10)

Vantagens

Assíncrono (Fire and Forget)

Desacoplamento entre aplicações

Resiliência

(11)

Globalcode – Open4education

Vantagens

Assíncrono (Fire and Forget)

Desacoplamento entre aplicações

Resiliência

(12)

Protocolos existentes

AMQP (0.9.1 e 1.0)

MQTT

Stomp

gRPC

HTTP

OpenWire

(13)

Globalcode – Open4education

APIs

JMS (1.0 e 2.0)

Qpid, active-client, rabbitmq-jms

Spring AMQP

RabbitMQ

Spring JMS

Vendor specific

cloudevents

(14)
(15)

Globalcode – Open4education

(16)

@ApplicationScoped

public class Producer { @Inject

ConnectionFactory connectionFactory;

private final Random random = new Random();

private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

private final Logger logger = LoggerFactory.getLogger(Producer.class);

public void onStart(@Observes StartupEvent ev) { scheduler.scheduleWithFixedDelay(() -> {

publishMessage();

}, 0L, 2L, TimeUnit.SECONDS); }

public void onStop(@Observes ShutdownEvent ev) { scheduler.shutdown();

}

private void publishMessage() {

try (JMSContext context = connectionFactory.createContext()) { String value = Integer.toString(random.nextInt(100));

(17)

@ApplicationScoped

public class Consumer { @Inject

ConnectionFactory connectionFactory;

private final ExecutorService scheduler = Executors.newSingleThreadExecutor(); private final Logger logger = LoggerFactory.getLogger(Consumer.class);

public void onStart(@Observes StartupEvent ev) {

scheduler.submit(() -> { consumeMessage(); });

}

public void onStop(@Observes ShutdownEvent ev) {

scheduler.shutdown(); }

private void consumeMessage() {

try (JMSContext context = connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE);

JMSConsumer consumer = context.createConsumer(context.createQueue("values-queue"));) { while (true) {

Message message = consumer.receive();

logger.info("Value: " + message.getBody(String.class)); }

} catch (JMSException e) { throw new RuntimeException(); }

} }

(18)

INFO [tdc.Producer] (pool-5-thread-1) Publishing value: 70 INFO [tdc.Consumer] (pool-6-thread-1) Value: 70

INFO [tdc.Producer] (pool-5-thread-1) Publishing value: 58 INFO [tdc.Consumer] (pool-6-thread-1) Value: 58

INFO [tdc.Producer] (pool-5-thread-1) Publishing value: 2 INFO [tdc.Consumer] (pool-6-thread-1) Value: 2

INFO [tdc.Producer] (pool-5-thread-1) Publishing value: 5 INFO [tdc.Consumer] (pool-6-thread-1) Value: 5

(19)

Globalcode – Open4education

(20)

INFO [tdc.Producer] (pool-8-thread-1) Publishing message: 3 INFO [tdc.FirstConsumer] (pool-7-thread-1) Value: 3

INFO [tdc.Producer] (pool-8-thread-1) Publishing message: 34 INFO [tdc.SecondConsumer] (pool-6-thread-1) Value: 34

INFO [tdc.Producer] (pool-8-thread-1) Publishing message: 43 INFO [tdc.FirstConsumer] (pool-7-thread-1) Value: 43

INFO [tdc.Producer] (pool-8-thread-1) Publishing message: 64 INFO [tdc.SecondConsumer] (pool-6-thread-1) Value: 64

(21)

Globalcode – Open4education

(22)

INFO [tdc.Producer] (pool-6-thread-1) Publishing value: 70 INFO [tdc.FirstConsumer] (pool-8-thread-1) Value: 70

INFO [tdc.SecondConsumer] (pool-7-thread-1) Value: 70 INFO [tdc.Producer] (pool-6-thread-1) Publishing value: 18 INFO [tdc.SecondConsumer] (pool-7-thread-1) Value: 18 INFO [tdc.FirstConsumer] (pool-8-thread-1) Value: 18

INFO [tdc.Producer] (pool-6-thread-1) Publishing value: 29 INFO [tdc.FirstConsumer] (pool-8-thread-1) Value: 29

(23)

Globalcode – Open4education

(24)
(25)

INFO --- [scheduling-1] tdc.Publisher: Publishing value 28 to Routing Key lazy.orange.elephant INFO --- [ntContainer#0-1] tdc.FirstConsumer: Value: 28

INFO --- [ntContainer#1-1] tdc.SecondConsumer: Value: 28

INFO --- [scheduling-1] tdc.Publisher: Publishing value 61 to Routing Key quick.orange.fox INFO --- [ntContainer#1-1] tdc.SecondConsumer: Value: 61

INFO --- [scheduling-1] tdc.Publisher: Publishing value 39 to Routing Key lazy.brown.fox INFO --- [ntContainer#0-1] tdc.FirstConsumer: Value: 39

INFO --- [scheduling-1] tdc.Publisher: Publishing value 52 to Routing Key lazy.pink.rabbit INFO --- [ntContainer#0-1] tdc.FirstConsumer: Value: 52

INFO --- [scheduling-1] tdc.Publisher: Publishing value 65 to Routing Key quick.brown.fox INFO --- [scheduling-1] tdc.Publisher: Publishing value 43 to Routing Key quick.orange.rabbit INFO --- [ntContainer#0-1] tdc.FirstConsumer: Value: 43

(26)

Sugestões de leitura

Enterprise Integration Patterns

Gregor Hohpe; Bobby Woolf

Documentação RabbitMQ

Especificação AMQP 0.9.1

Especificação JMS 2.0

(27)

Globalcode – Open4education

(28)

Referências

Documentos relacionados

Foram quantificados os teores de 2-tridecanona (2-TD), 2-undecanona (2-UD), N, P, K, Ca e Mg, densidade e tipos de tricomas e tamanho das folhas nos terços apical, mediano e basal

Lineu Prestes Av., Bl. Previous studies have demonstrated that rutin, a bioactive compound, interacts with filters incorporated in sunscreens. Therefore, this work aimed

Destacam-se ainda como objetivos: identificar como as mudanças no mundo do trabalho têm rebatido na prática do assistente social na Estratégia Saúde da Família, compreender os limites

1e, 1f, 1g Distribution and ecology: Trees or shrubs, 1.5 − 18 m high, occurring in Brazil, from southern Bahia, to central Rio de Janeiro, mainly in coastal vegetation (Fig..

• Rever as ações estratégicas dos anos anteriores nas atividades e tarefas de compreensão auditiva, audiovisual e escrita, adaptando-as aos novos documentos e situações

Como uma falta não pode ser diretamente detectada pelo sistema, o ponto inicial para as técnicas de tolerância a faltas é a ›detecção de um estado errôneo

lhante; linhas vasculares bem v1S1veis, cheias de conteúdo amarelo-pardo, em geral retas; pa- rênquima axial moderadamente abundante, pa- ratraqueal em faixas; raios

De seguida, é descrito o seu funcionamento, nomeadamente a gestão dos medicamentos e outros produtos farmacêuticos - gestão dos produtos farmacêuticos e a sua