-
Introduction to reactive programmingEngineering 2023. 3. 20. 20:32728x90
Reactive programming은 다소 생소하고 접해보지 못 한 용어지만 언젠가 유용하게 적용, 활용해 볼 수 있는 기법 정도로 생각되어 여기 기록하게 되었다.
정의는 대략 이렇다.
Reactive programming is about dealing with data streams and the propagation of change.
시스템에서 유동적으로 피드백을 주는 느낌으로 생각이 되며 실시간 통신이 필요한 어플리케이션에 적용하게 된다면 상당히 유용할 것으로 생각된다.
2개의 Agent로 운영되며 다음 그림과 같이 동작한다.
다음 세 가지 기능, 옵션으로 동작하며 각각 동작 시, 완료 시, 오류 발생 시 해당 기능이 수행된다.
- OnNext: Whenever the Observable emits an event, this method is called on our Observer, which takes as parameter the object emitted so we can perform some action on it.
- OnCompleted: This method is called after the last call of the onNext method, indicating that the sequence of events associated with an Observable is complete and it has not encountered any errors.
- OnError: This method is called when it has encountered some error to generate the expected data, like an unhandled exception.
여기서 'Operator'라는 용어가 사용되는데 일종의 함수(기능)으로 보면 된다. observable이 발생시키는 모든 소스 성분에 대해 한 observable에서 다른 observable로 이벤트 발생에 대해 데이터의 흐름을 체인 내에서 발생시킨다.
위의 세 가지 기능, 옵션과 마찬가지로 Reactive operators는 다음의 주요 라이브러리들을 갖는다. 주로 데이터 전달, 변환, 제거 등의 기능을 수행하는 것으로 보인다.
- map : transforms items emitted by an Observable by applying a function to each them.
- flatMap : transforms the objects emitted by an Observable into Observables (“nested Observables”), then flatten the emissions from those into a single Observable.
- filter : emits only items from an Observable that pass a predicate test.
- just : converts objects into an Observable that emits those objects.
- takeWhile : discards items emitted by an Observable after a specified condition becomes false.
- distinct : suppresses duplicate objects emitted by an Observable.
자바 기반의 예제코드가 있으니 참고해도 좋을 듯 하다.
반응형'Engineering' 카테고리의 다른 글
(문헌조사) OPC UA 기술 (1) (0) 2023.06.03 Azure 데이터 팩토리 관련 내용 (0) 2023.05.28 (정보) SAP MII에 대해 (1) 2023.05.27 (Raspberry pi) CAN BUS communication with MCP2515/RS485 CAN HAT (0) 2023.03.20 Integration of CAD, CAM and NC with Step-NC (pythonocc) (0) 2023.03.04