Before we go forward with an example, I'd like to remind you that this isn't Erlang, and that yanking the rug out from under a library and shoving in a new one could potentially lead to disastrous results if you haven't planned ahead in your program design.
If your library is purely functional with no side effects, then updating it at runtime is going to be much safer than swapping in a file containing mutable data. There's also an issue of atomicity to consider. I've come to find out that reloading a library is not an atomic operation. This could potentially cause some issues depending on your application structure. The def function and defn macro are both atomic which will prevent the possibility of partially defined functions creeping in, but you'll need to watch out for functions that depend on each other being temporarily out of date. With the warnings out of the way, let's get to it.
The easiest way to update a running program is to embed a remote REPL into your code. This is a relatively simple thing to do since Clojure's builtin repl function is designed to allow custom input and output streams. The clojure.contrib.server-socket library actually includes some code to assist with spawning a remote REPL, but I tend to take the hard road everywhere, so I just rolled my own solution.
; file: repl_server.clj
(ns repl-server
(:import
(java.net ServerSocket Socket)
(java.io InputStreamReader PrintWriter)
(java.util.concurrent ThreadPoolExecutor TimeUnit LinkedBlockingQueue)
(java.net BindException)
(clojure.lang LineNumberingPushbackReader)))
(defn bind-server [port backlog]
"binds to the given port with the backlog specified"
(new #^ServerSocket ServerSocket port backlog))
(defn server-loop [tasklet socket min-threads max-threads]
"accepts connections and delegates them to the specified task"
(let [exec (ThreadPoolExecutor. min-threads max-threads 60 TimeUnit/SECONDS
(LinkedBlockingQueue.))]
(loop []
(let [accepted-socket (.accept #^ServerSocket socket)]
(.submit exec #^Callable #(with-open [accepted-socket accepted-socket]
(tasklet accepted-socket))))
(recur))))
(defn spawn-repl [socket]
"spawns a repl on a given socket"
(let [input (new LineNumberingPushbackReader
(new InputStreamReader (.getInputStream socket)))
output (new PrintWriter (.getOutputStream socket) true)]
(binding [*in* input
*out* output
*err* output]
(clojure.main/repl))))
(def #^ServerSocket *ss* (bind-server 12345 25))
(def repl-server (future (server-loop spawn-repl *ss* 5 100)))
Here we have a simple REPL server ready for embedding into any program we see fit. It runs in a loop accepting connections and then invoking spawn-repl for any client that connects. We use the binding form to set *in*, *out*, and *err* temporarily and then return them to their original values. We set the server to run on port 12345 and then run it in it's own thread via a call to future. It's worth noting that setting the port and actually invoking the server would be better done elsewhere, but this is just a trivial example, so don't crucify me.
Next we'll create a stupidly simple library which we'll be hot swapping.
; file: greeting.clj (ns greeting) (def message "hello world") (defn print-message [] (println "message:" message))We follow this with a main file that ties it all together.
(ns main
(:use greeting
repl-server))
(defn main []
(loop []
(print-message)
(Thread/sleep 1000)
(recur)))
(main)
The main function simply runs in an infinite loop calling print-message every one second. From here we're ready to test some hot code swapping.
$ clj main.clj message: hello world message: hello world ...So far so good, now let's connect to the REPL.
$ nc localhost 12345 clojure.core=> (ns greeting) nil greeting=> (def message "hola amigo!") #'greeting/messageAt this point the main window is now printing the updated message.
message: hola amigo! message: hola amigo! ...Now let's make a change to greeting.clj.
; file: greeting.clj (ns greeting) (def message "8675309") (defn print-message [] (println "jenny:" message))With the new file in place, we request a reload. Keep in mind that this isn't atomic (as previously stated), but it is convenient for loading larger chunks of code.
$ nc localhost 12345 clojure.core=> (require 'greeting :reload)The output of our main loop confirms that the reload worked as intended.
jenny: 8675309 jenny: 8675309 jenny: 8675309 ...This has obviously been a dead-simple example, but it should demonstrate the flexibility that Clojure provides when it comes to changing things at runtime.
What about redefining structures? Are the instances updated? You can do it in CLOS
ReplyDeleteGreat post! I am see the great contents and step by step read really nice information.I am gather this concepts and more information. It's helpful for me my friend. Also great blog here with all of the valuable information you have.
ReplyDeletePHP Training in Chennai
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training
Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definitely interested in this one. Just thought that I would post and let you know. Nice! thank you so much! Thank you for sharing.
ReplyDeletelenovo service center in chennai
lenovo mobile service center in chennai
lenovo service centre chennai
Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
ReplyDeleteWeb Designing Training in Chennai | No.1 Web Designing Course Training in Chennai
SQL Server Training in Chennai | Best No.1 SQL Server Course Training in Chennai
Digital Marketing Training in Chennai | Best Digital Marketing Course Training in Chennai
Java Training in Chennai | Best Java Training in Chennai
C C++ Training in Chennai | Best C C++ Training in Chennai
And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
ReplyDeleteJava Training in Chennai |Best Java Training course in Chennai
C C++ Training in Chennai |Best C C++ Training course in Chennai
Data science Course Training in Chennai |Best Data Science Training course in Chennai
RPA Course Training in Chennai |Best RPA Training course in Chennai
AWS Course Training in Chennai |Best AWS Training course in Chennai
Devops Course Training in Chennai |Best Devops Training course in Chennai
Selenium Course Training in Chennai |Best Selenium Training course in Chennai
Java Course Training in Chennai | Best Java Training course in Chennai
Web Designing Training in Chennai | Best Web Designing Training course in Chennai
MATLAB TRAINING IN CHENNAI | Best MATLAB TRAINING course IN CHENNAI
Thanks for sharing this great article. It made me understand few things about this concept which I never knew before. Keep posting such great articles so that I gain from it. Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai
ReplyDeleteFlying Shift - Packers & Movers in Bhopal
ReplyDeleteReally nice post. Thank you for sharing amazing information.
ReplyDeletePython training in Chennai/Python training in OMR/Python training in Velachery/Python certification training in Chennai/Python training fees in Chennai/Python training with placement in Chennai/Python training in Chennai with Placement/Python course in Chennai/Python Certification course in Chennai/Python online training in Chennai/Python training in Chennai Quora/Best Python Training in Chennai/Best Python training in OMR/Best Python training in Velachery/Best Python course in Chennai
Great post very useful info thanks for this post ....
ReplyDeleteAws training chennai | AWS course in chennai
Great post very useful info thanks for this post ....
ReplyDeleteAws training chennai | AWS course in chennai
digital marketing & web development company in bhopal
ReplyDeleteSuch a great information for blogger iam a professional blogger thanks…
ReplyDeleteUpgrade your career Learn Oracle Training from industry experts gets complete hands on Training, Interview preparation, and Job Assistance at Softgen Infotech.
inplant training in chennai
ReplyDeleteinplant training in chennai
inplant training in chennai for it.php
brunei darussalam web hosting
costa rica web hosting
costa rica web hosting
hong kong web hosting
jordan web hosting
turkey web hosting
gibraltar web hosting
nice........
ReplyDeleteinplant training in chennai
inplant training in chennai
online python internship
online web design
online machine learning internship
online internet of things internship
online cloud computing internship
online Robotics
online penetration testing
good blogs.....!!!
ReplyDeletechile web hosting
colombia web hosting
croatia web hosting
cyprus web hosting
bahrain web hosting
india web hosting
iran web hosting
kazakhstan web hosting
korea web hosting
moldova web hosting
nice information....!!
ReplyDeletetext animation css
animation css background
sliding menu
hover css
css text animation
css loaders
dropdown menu
buttons with css
very good.....
ReplyDeleteinternship in bangalore for cse students
internship for aerospace engineering students in india
core companies in coimbatore for ece internship
paid internship in pune for computer engineering students
automobile internship in chennai
internship in chennai for eee with stipend
internship for bca students
dotnet training in chennai
aeronautical engineering internship
inplant training for ece students
good..nice..
ReplyDeleteinternships in bangalore for ece students 2019
internship for aeronautical engineering students in bangalore
kaashiv infotech chennai
internship for ece students in bangalore 2018
internship in chennai for eee with stipend
internship in chennai for mechanical engineering students
kaashiv infotech hyderabad
kaashiv infotech internship
internship in chennai for cse 2019
internship in aeronautical engineering
This piece of information's are really Wonderful...The information's are helpful to enhance the careers...Good Works!!!
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
You must take part in a contest for top-of-the-line blogs on the web. I will suggest this web site!
ReplyDeleteJava online online training
Machine learning online online training
Magento online online training
MySql Admin online online training
Wonderful article, very useful and well explanation. Your post is extremely incredible. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
ReplyDeleteJava Training in Chennai
Java Training in Velachery
Java Training inTambaram
Java Training in Porur
Java Training in Omr
Java Training in Annanagar
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeletePython Training in Chennai
Python Training in Velachery
Python Training in Tambaram
Python Training in Porur
Python Training in Omr
Python Training in Annanagar
Great post! I am see the great contents and step by step read really nice information.I am gather this concepts and more information.
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Training in Porur
Digital Marketing Training in Omr
Digital MarketingTraining in Annanagar
such a nice post thanks for sharing this with us really so impressible and attractive post
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Training in Velachery
Software Testing Training in Tambaram
Software Testing Training in Porur
Software Testing Training in Omr
Software Testing Training in Annanagar
very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
ReplyDeleteDevOps Training in Chennai
DevOps Course in Chennai
such a nice post thanks for sharing this with us really so impressible and attractive postsalesforce training in chennai
ReplyDeletesoftware testing training in chennai
robotic process automation rpa training in chennai
blockchain training in chennai
devops training in chennai
no deposit bonus forex 2021 - takipçi satın al - takipçi satın al - takipçi satın al - takipcialdim.com/tiktok-takipci-satin-al/ - instagram beğeni satın al - instagram beğeni satın al - google haritalara yer ekleme - btcturk - tiktok izlenme satın al - sms onay - youtube izlenme satın al - google haritalara yer ekleme - no deposit bonus forex 2021 - tiktok jeton hilesi - tiktok beğeni satın al - binance - takipçi satın al - uc satın al - finanspedia.com - sms onay - sms onay - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - perde modelleri - instagram takipçi satın al - instagram takipçi satın al - cami avizesi - marsbahis
ReplyDeleteinstagram takipçi satın al
ReplyDeleteinstagram takipçi satın al
takipçi satın al
takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
takipçi satın al
ReplyDeleteinstagram takipçi satın al
https://www.takipcikenti.com
marsbahis
ReplyDeletebetboo
sultanbet
marsbahis
betboo
sultanbet