I am using langohr to connect rabbitmq, if I don't specify any connection string, it works well and it connects to local server, but I want to connect to remote server. So I have the following code. I am using connection string of amqp://bigdata:bigdata#s1:5672, s2 is the hostname of the remote server.
let [ a (println rabbitmq-url)
rmq-conn (rmq/connect {:uri rabbitmq-url})
a (println rabbitmq-url)]
But it throws the error of the following
$ lein run
amqp://bigdata:bigdata#s1:5672
Exception in thread "main" java.io.IOException, compiling:(/tmp/form-init589039011205967992.clj:1:71)
at clojure.lang.Compiler.load(Compiler.java:7142)
at clojure.lang.Compiler.loadFile(Compiler.java:7086)
at clojure.main$load_script.invoke(main.clj:274)
at clojure.main$init_opt.invoke(main.clj:279)
at clojure.main$initialize.invoke(main.clj:307)
at clojure.main$null_opt.invoke(main.clj:342)
at clojure.main$main.doInvoke(main.clj:420)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:383)
at clojure.lang.AFn.applyToHelper(AFn.java:156)
at clojure.lang.Var.applyTo(Var.java:700)
at clojure.main.main(main.java:37)
Caused by: java.io.IOException
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:376)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:36)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:83)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:609)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:651)
at com.novemberain.langohr.Connection.init(Connection.java:92)
at langohr.core$connect.invoke(core.clj:93)
at clojurewerkz.testcom.core$create_message_from_database.invoke(core.clj:33)
at clojurewerkz.testcom.core$create_message_from_database_loop.invoke(core.clj:53)
at clojurewerkz.testcom.core$_main.doInvoke(core.clj:60)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at clojure.lang.Var.invoke(Var.java:375)
at user$eval5.invoke(form-init589039011205967992.clj:1)
at clojure.lang.Compiler.eval(Compiler.java:6703)
at clojure.lang.Compiler.eval(Compiler.java:6693)
at clojure.lang.Compiler.load(Compiler.java:7130)
... 11 more
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)
... 27 more
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:288)
at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:95)
at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:139)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:534)
at java.lang.Thread.run(Thread.java:745)
First, make sure that RabbitMQ is running on the remote server. Second, check if the user that is connecting to that instance has the permission to do so.
The same issue as below: my account doesn't have access to virtual hosts.
RabbitMQ new connection refused due to SocketException
try this code.
First, put these libs into your project.clj
;;rabbitmq client
[com.novemberain/langohr "2.11.0"]
;;logging
[org.clojure/tools.logging "0.2.4"]
Then, make necessary imports in your clj file:
(:require
;logging
[clojure.tools.logging :as log]
;;rabbitmq
[langohr.core :as rmq]
[langohr.channel :as lch]
[langohr.queue :as lq]
[langohr.exchange :as le]
[langohr.basic :as lb] ))
And here is connect function:
(defn get-mq-connect
"This function connects to RabbitMQ.
params example: {:host \"localhost\" :port 5672 :username \"guest\" :password \"guest\" :vhost \"/\"}
Returns: connect object - connection successful, nil - error occured."
[params]
(try
(log/trace params)
(rmq/connect params)
(catch Exception e
(log/error (.getMessage e))
(println "MQ connection error:" (.getMessage e)))))
You can call this function that way:
(let [ params {:host "myhost.com" :port 5672 :username "admin" :password "StrongPassw098" :vhost "/"}
conn (get-mq-connect mq-params)]
(when-not (nil? conn)
(let [ch (lch/open conn)]
(while (not #stop-flag?)
(do
;;put here your code
))
(rmq/close ch)
(rmq/close conn)))))
Related
I tried the following code to create a datomic database and connect to it:
(let [cfg (-> env :datomic-cfg)
client (d/client cfg)]
(do
(d/create-database
client
{:db-name "humboi-march-2021"})
(d/connect client {:db-name "humboi-march-2021"})))
However, I’m getting the following error:
Execution error (ExceptionInfo) at datomic.client.impl.pro/create-spi (pro.clj:72).
Invalid connection config: {:server-type :peer-server, :access-key "key-0680cb34675d5fd59", :secret "<ELIDED>", :endpoint "http://entry.humboi-2021.us-east-1.datomic.net:8182", :validate-hostnames false}
How to fix this?
Your uri has to be a string, for example I use:
(let [uri "datomic:free://localhost:4334/name"]
(d/create-database uri)
(d/connect uri))
I am trying to create file upload system. However, i stuck and can't resolve the problem.
My core.cjl
(ns hiccup-templating.core
(:require [compojure.core :refer [defroutes GET ANY POST]]
[compojure.route :as route]
[compojure.handler :as handler]
[ring.adapter.jetty :as jetty]
[hiccup-templating.views.layout :as layout]
[hiccup-templating.views.contents :as contents]
(ring.middleware [multipart-params :as mp])
(clojure.contrib [duck-streams :as ds])))
(defn upload-file
[file]
(ds/copy (file :tempfile) (ds/file-str (str (rand-int 30) ".jpg")))
(layout/application "Home" (contents/succes)))
(defroutes public-routes
(GET "/" [] (layout/application "Home" (contents/index)))
(mp/wrap-multipart-params
(POST "/file" {params :params} (upload-file (get params "file")))))
(def application (handler/site public-routes))
(defn -main []
(let [port (Integer/parseInt (or (System/getenv "PORT") "8080"))]
(jetty/run-jetty application {:port port :join? false})))
So i have such an error.
2017-04-25 17:00:57.934:WARN:oejs.AbstractHttpConnection:/file
java.lang.NullPointerException
at hiccup_templating.core$upload_file.invoke(core.clj:14)
at hiccup_templating.core$fn__3152.invoke(core.clj:20)
at compojure.core$make_route$fn__489.invoke(core.clj:94)
at compojure.core$if_route$fn__473.invoke(core.clj:40)
at compojure.core$if_method$fn__466.invoke(core.clj:25)
at ring.middleware.multipart_params$wrap_multipart_params$fn__947.invoke(multipart_params.clj:107)
at compojure.core$routing$fn__495.invoke(core.clj:107)
at clojure.core$some.invoke(core.clj:2443)
at compojure.core$routing.doInvoke(core.clj:107)
at clojure.lang.RestFn.applyTo(RestFn.java:139)
at clojure.core$apply.invoke(core.clj:619)
at compojure.core$routes$fn__499.invoke(core.clj:112)
at ring.middleware.keyword_params$wrap_keyword_params$fn__854.invoke(keyword_params.clj:32)
at ring.middleware.nested_params$wrap_nested_params$fn__903.invoke(nested_params.clj:70)
at ring.middleware.params$wrap_params$fn__820.invoke(params.clj:58)
at ring.middleware.multipart_params$wrap_multipart_params$fn__947.invoke(multipart_params.clj:107)
at ring.middleware.flash$wrap_flash$fn__2119.invoke(flash.clj:31)
at ring.middleware.session$wrap_session$fn__2099.invoke(session.clj:85)
at ring.adapter.jetty$proxy_handler$fn__2209.invoke(jetty.clj:18)
at ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$0.handle(Unknown Source)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:363)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:483)
at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:931)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:992)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:856)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:745)
I will be glad if somebody give me the way how to understand the problem i have.
I also had this problem
Cannot find the path you want to copy the file to.
Make sure you follow the path.
I am using Linux operating system and I gave this address and the problem was solved.
(copy actual-file (File. (format "/home/naser/Desktop/%s" file-name)))
I'm using the clojure rethinkdb library https://github.com/apa512/clj-rethinkdb - here's my connection snippet:
(ns rethink.core
(:require
[environ.core]
[cheshire.core :as json]
[org.httpkit.client :as http]
[rethinkdb.query :as r]))
(defn rethink-connect
[]
(r/connect :host (environ.core/env :rethinkdb-host)
:port (environ.core/env :rethinkdb-port)
:db (environ.core/env :rethinkdb-db)
:auth-key (environ.core/env :rethinkdb-auth)))
(defn record-event!
[event collection]
(let [r (with-open [conn (rethink-connect)]
(-> (r/table collection)
(r/insert [event])
(r/run conn)))]))
But I keep getting the error:
clojure.lang.ExceptionInfo: Error connecting to RethinkDB database
core.clj:4617 clojure.core/ex-info
core.clj:4617 clojure.core/ex-info
core.clj:88 rethinkdb.core/connect
core.clj:43 rethinkdb.core/connect
RestFn.java:619 clojure.lang.RestFn.invoke
core.clj:10 rethink.core/rethink-connect
core.clj:8 rethink.core/rethink-connect
core.clj:17 rethink.core/record-event!
core.clj:15 rethink.core/record-event!
core.clj:23 webhook.core/record-event!
core.clj:21 webhook.core/record-event!
handler.clj:11 webhook.handler/webhook-post
handler.clj:8 webhook.handler/webhook-post
handler.clj:9 app.handler/fn
handler.clj:9 app.handler/fn
core.clj:135 compojure.core/make-route[fn]
core.clj:122 compojure.core/wrap-route-middleware[fn]
core.clj:126 compojure.core/wrap-route-info[fn]
core.clj:45 compojure.core/if-route[fn]
core.clj:27 compojure.core/if-method[fn]
core.clj:151 compojure.core/routing[fn]
core.clj:2592 clojure.core/some
core.clj:2583 clojure.core/some
core.clj:151 compojure.core/routing
core.clj:148 compojure.core/routing
RestFn.java:139 clojure.lang.RestFn.applyTo
core.clj:648 clojure.core/apply
core.clj:641 clojure.core/apply
core.clj:156 compojure.core/routes[fn]
keyword_params.clj:35 ring.middleware.keyword-params/wrap-keyword-params[fn]
params.clj:64 ring.middleware.params/wrap-params[fn]
absolute_redirects.clj:38 ring.middleware.absolute-redirects/wrap-absolute-redirects[fn]
content_type.clj:30 ring.middleware.content-type/wrap-content-type[fn]
default_charset.clj:26 ring.middleware.default-charset/wrap-default-charset[fn]
not_modified.clj:52 ring.middleware.not-modified/wrap-not-modified[fn]
Var.java:379 clojure.lang.Var.invoke
reload.clj:22 ring.middleware.reload/wrap-reload[fn]
stacktrace.clj:23 ring.middleware.stacktrace/wrap-stacktrace-log[fn]
stacktrace.clj:86 ring.middleware.stacktrace/wrap-stacktrace-web[fn]
jetty.clj:20 ring.adapter.jetty/proxy-handler[fn]
(Unknown Source) ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$ff19274a.handle
HandlerWrapper.java:116 org.eclipse.jetty.server.handler.HandlerWrapper.handle
Server.java:369 org.eclipse.jetty.server.Server.handle
AbstractHttpConnection.java:486 org.eclipse.jetty.server.AbstractHttpConnection.handleRequest
AbstractHttpConnection.java:944 org.eclipse.jetty.server.AbstractHttpConnection.content
AbstractHttpConnection.java:1005 org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content
HttpParser.java:865 org.eclipse.jetty.http.HttpParser.parseNext
HttpParser.java:240 org.eclipse.jetty.http.HttpParser.parseAvailable
AsyncHttpConnection.java:82 org.eclipse.jetty.server.AsyncHttpConnection.handle
SelectChannelEndPoint.java:668 org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle
SelectChannelEndPoint.java:52 org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run
QueuedThreadPool.java:608 org.eclipse.jetty.util.thread.QueuedThreadPool.runJob
QueuedThreadPool.java:543 org.eclipse.jetty.util.thread.QueuedThreadPool$3.run
Thread.java:745 java.lang.Thread.run
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Character
RT.java:1177 clojure.lang.RT.intCast
tcp.clj:156 aleph.tcp/client
tcp.clj:126 aleph.tcp/client
core.clj:61 rethinkdb.core/connect
I'm getting the host "aws-us-xxx-1-portal.xxx.xxxx.com", port "15731", db "dev" and auth-key from my compose.io rethinkdb instance. The auth-key is the admin password taken from authentication credential provided by compose.io interface. Not sure what's going on.
Actually, I needed to use a :ca-cert parameter as well, with the certificate being provided from the compose.io web interface.
I am trying to run Aleph on top of Ring and use lein ring server for shorter feedback loop.
When I'm invoking lein ring server everything seems to be fine but when I point my browser to an url I get a nasty NullPointerException with the stack trace below.
However, when I run (al.app/start 3006) then no NLP shows up.
The whole project is available on GitHub.
What am I doing wrong?
core.clj:107 lamina.core/enqueue
app.clj:39 al.app/hello-handler
http.clj:79 aleph.http/wrap-aleph-handler[fn]
response.clj:27 compojure.response/eval1328[fn]
response.clj:10 compojure.response/eval1289[fn]
core.clj:93 compojure.core/make-route[fn]
core.clj:39 compojure.core/if-route[fn]
...
I am using aleph 0.3.2 and that's my code:
(ns al.app
(:use
aleph.http
lamina.core
compojure.core
ring.middleware.keyword-params)
(:require [compojure.route :as route]))
(defn hello-handler
"Our handler for the /hello path"
[ch request]
(let [params (:route-params request)
name (params :name)]
(enqueue ch
{:status 200
:headers {}
:body (str "Hello " name)})))
(defroutes my-routes
(GET ["/hello/:name", :name #"[a-zA-Z]+"] {} (wrap-aleph-handler hello-handler))
(route/not-found "Page not found"))
(defn start
"Start our server in the specified port"
[port]
(start-http-server (wrap-ring-handler my-routes) {:port port}))
The Lein-Ring plugin uses an embedded Jetty web server, while Aleph uses the asynchronous Netty web server. The aleph.http/wrap-aleph-handler middleware is designed only to work with a Netty server started with aleph.http/start-http-server function.
I'm writing a clojure app for internal use, and I want the config file to be in clojure too. I have defined a few macros to make writing the config file easier, but when I try to eval the data from the config file, it cant find my macros. This works fine from the REPL however. For example, I'm using
(load-string "/path/to/config")
I get this error:
Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: defcmd in this context, compiling:(null:1)
at clojure.lang.Compiler.analyze(Compiler.java:6235)
at clojure.lang.Compiler.analyze(Compiler.java:6177)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3452)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6411)
at clojure.lang.Compiler.analyze(Compiler.java:6216)
at clojure.lang.Compiler.analyze(Compiler.java:6177)
at clojure.lang.Compiler.eval(Compiler.java:6469)
at clojure.lang.Compiler.load(Compiler.java:6902)
at clojure.lang.Compiler.load(Compiler.java:6872)
at clojure.core$load_reader.invoke(core.clj:3625)
at clojure.core$load_string.invoke(core.clj:3635)
at serverStats.core$load_config.invoke(core.clj:67)
at serverStats.core$_main.doInvoke(core.clj:78)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at clojure.lang.Var.invoke(Var.java:397)
at user$eval109.invoke(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:6465)
at clojure.lang.Compiler.eval(Compiler.java:6455)
at clojure.lang.Compiler.eval(Compiler.java:6431)
at clojure.core$eval.invoke(core.clj:2795)
at clojure.main$eval_opt.invoke(main.clj:296)
at clojure.main$initialize.invoke(main.clj:315)
at clojure.main$null_opt.invoke(main.clj:348)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:405)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: Unable to resolve symbol: defcmd in this context
at clojure.lang.Util.runtimeException(Util.java:156)
at clojure.lang.Compiler.resolveIn(Compiler.java:6720)
at clojure.lang.Compiler.resolve(Compiler.java:6664)
at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6625)
at clojure.lang.Compiler.analyze(Compiler.java:6198)
... 28 more
However, running that same command from the REPL in my namespace works fine.
You probably want some more sophisticated loading scheme. I assume you want to put the configuration into a dedicated configuration namespace. It will only contain the configuration. Helper functions are held in a separate namespace use'd in the configuration namespace.
(defn setup-config-space
[]
(binding [*ns* *ns*]
(in-ns 'config.namespace)
(refer-clojure)
(use 'config.helpers)))
(defn load-config
[path]
(binding [*ns* *ns*]
(in-ns 'config.namespace)
(load-file path)))
See the example use:
..ojure/1.4.0-alpha3% cat config/helpers.clj
(ns config.helpers)
(defmacro defcmd
[x]
`(defn ~x [] "Hello"))
..ojure/1.4.0-alpha3% cat x.clj
(defcmd foo)
..ojure/1.4.0-alpha3% java -cp .:clojure-1.4.0-alpha3.jar clojure.main -r
Clojure 1.4.0-alpha3
user=> ; Paste above functions
#'user/setup-config-space
#'user/load-config
user=> (setup-config-space)
nil
user=> (load-config "x.clj")
#'config.namespace/foo
user=> (config.namespace/foo)
"Hello"