I have an application based on Java and Spring (Springboot, Spring-reactive, Spring-Kafka) witch continuously consumes information from a Kafka-topic and store the data with a key in a ConcurrentHashMap (via a simple wrapper). The application also contains an REST-API for fetching streaming information using Reactive Flux.
I would like to come up with a way of where it is possible to call the API for data from the map (using a key), where the response is a stream of the currently associated value from the map together with subsequent changes to the value (as updated from the topic) i.e without closing the stream.
It feels like this should be possible using maby a PropertyChangeListener combined with a Flux.Generate but my reactive skills are to weak to see how I should achieve this. Ive done some tries but I cant see how to get the Generator to emit on PropertyChangeEvents.
Would this be possible?
If anyone could provide me with a example for this, or maby point me to one online it would be much appreciated.
BR
Related
dear stackoverflow community.
Currently, I am working on a project. This project shall have a server and clients connecting to it. Because of their simplicity, I'd like to use Java's integrated ServerSockets and Sockets.
In my project, data shall be sent from the client to the server and opposite.
My initial idea is to just send JSON and then parse it as receiver and get the data from that.
I'm a little unsure about that though, since JSON isn't something that's integrated into Java, but comes from Java script. Also, I'm currently using a Multithreaded-Socket-Server, so I have a ClientHandler Thread class. In that class, the messages were received, parsed and the "action" parameter was read out of the JSON and then I did a switch statement with multiple actions and their functions. I don't think that's a good way of doing that either.
So, my question is:
How can I do that better, and maybe do I have to use something else?
Thanks in advance.
It is true that JSON grew out of JavaScript, but it is a reasonable definition language on its own and I don't see any reason you shouldn't use it. There are libraries for parsing it so you don't have to.
Assuming your JSON structures are different for different purposes, and complex enough to need different classes to represent them, I like the idea of the JSON having a parameter that identifies the class to which it belongs, after which you can hand off parsing to a class that understands the designated output. Then a class can read the JSON, get the type, and some the specific parsing routine can go from there to an object created for the purpose.
I don't see anything wrong with an action string, either; it's served well enough for Swing and some other UIs, after all. Instead of branching out to a function, depending on complexity again, you could have action classes that all implemented an interface, and the action 'verb' could tell you which one (out of a map, say?) to get and execute the 'performAction()' method on or whatever you want to call it.
I don't know how clear this is from a quick description; would be willing to chat about it in an SO chat room if you care about it.
I need some help with some modification of https://github.com/playframework/play-java-websocket-example.
This example based on own Source for given Stock ID. Every Source generate new stock value in some time interval:
public Source<StockUpdate, NotUsed> update() {
return source.throttle(1, duration, 1, ThrottleMode.shaping())
.map(sq -> new StockUpdate(sq.symbol, sq.price));
}
The example in much part is what I need (the "watch/unwatch" stocks idea) so I want to use it in very similiar way but I don't want to fake stock quote time interval generator in my app because I'm connected to real, one stock exchange source.
I read messages (in other part of my app) via java.io.InputStream and, for now, I put them in java.util.Queue (java.util.concurrent.LinkedBlockingQueue) via queue.offer(message) and now I want to get it somehow from Stock.update() (however I don't know it's best place).
So far I got know that I could use for example Source.queue(BUFFER_LENGTH, OverflowStrategy.backpressure()), but it doesn't have any reference to (my) queue.
I also know that I could use Source.queue(BUFFER_LENGTH, OverflowStrategy.backpressure()).to(...).run(...).offer(...) but unfortunatelly I don't know how to connect it in UserActor.addStock(Stock) with Flow, UniqueKillSwitch, etc.
Maybe someone needed modifications like me and could give some hints?
So far I got know that I could use for example Source.queue(BUFFER_LENGTH, OverflowStrategy.backpressure()), but it doesn't have any reference to (my) queue
You could change your code so it uses the Source.queue instead of the java.util.Queue. Alternatively you could have a thread to read from your queue and push it to the Source.queue.
As far as I know Stream API is intended to be applied on collections. But I like the idea of them so much that I try to apply them when I can and when I shouldn't.
Originally my app had two threads communicating through BlockingQueue. First would populate new elements. Second make transformations on them and save on disk. Looked like a perfect stream oportunity for me at a time.
Code I ended up with:
Stream.generate().flatten().filter().forEach()
I'd like to put few maps in there but turns out I have to drag one additional field till forEach. So I either have to create meaningless class with two fields and obscure name or use AbstractMap.SimpleEntry to carry both fields through, which doesn't look like a great deal to me.
Anyway I'd rewritten my app and it even seems to work. However there are some caveats. As I have infinite stream 'the thing' can't be stopped. For now I'm starting it on daemon thread but this is not a solution. Business logic (like on connection loss/finding, this is probably not BL) looks alienated. Maybe I just need proxy for this.
On the other hand there is free laziness with queue population. One thread instead of two (not sure how good is this). Hopefully familiar pattern for other developers.
So my question is how viable is using of Stream API for application flow organising? Is there more underwather roks? If it's not recomended what are alternatives?
I am implementing a batch program in java. The flow is as follows: I fetch data from a database convert the data into custom objects and then put those objects in a queue. Then the goal is to run some profiling logic (for example nlp) afterwards. A friend of mine told me I should consider using the java stream api since it supports parallel processing. I am relatively new to Java 8 so my question is where to put(or execute) the mentioned profiling logic? Is there a way to create custom operations or do I have to implement a custom Collector?
Thank you in advance.
As mentioned in the comments by #MarkoTopolnik map is the solution.
In our Java app, using spring-aop, in order to collect certain statistics, we have two around-advices which get hit consecutively per request, like this: advice-A ->proceed-> advice-B ->proceed-> advice-B -> advice-A
To avoid writing to the DB twice, we want to share the information captured by the second advice with the first, and write it all to the DB after the proceed in the first (outer) advice.
How can this be done without using nasty things like static classes with ThreadLocals?
I read about perthis and pertarget, but if I understand it correctly that would only be useful if our service beans are request-scoped. Then you could wire one advice into the other and share information that way.