Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
KiekPAD-Viz-Provider
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sören Henning
KiekPAD-Viz-Provider
Commits
4fa6b578
Commit
4fa6b578
authored
8 years ago
by
Sören Henning
Browse files
Options
Downloads
Patches
Plain Diff
made CassandraService more generic with adding application.properties
parent
da88fb35
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/kiekpad/vizprovider/service/CassandraService.java
+52
-11
52 additions, 11 deletions
...in/java/kiekpad/vizprovider/service/CassandraService.java
with
52 additions
and
11 deletions
src/main/java/kiekpad/vizprovider/service/CassandraService.java
+
52
−
11
View file @
4fa6b578
package
kiekpad.vizprovider.service
;
package
kiekpad.vizprovider.service
;
import
java.time.Duration
;
import
java.time.Instant
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
com.datastax.driver.core.Cluster
;
import
com.datastax.driver.core.Cluster
;
...
@@ -10,23 +15,59 @@ import com.datastax.driver.core.exceptions.NoHostAvailableException;
...
@@ -10,23 +15,59 @@ import com.datastax.driver.core.exceptions.NoHostAvailableException;
@Service
@Service
public
class
CassandraService
{
public
class
CassandraService
{
private
static
final
String
IP_ADDRESS
=
"192.168.99.100"
;
//
private static final String IP_ADDRESS = "192.168.99.100";
private
static
final
int
PORT
=
327
86
;
//
private static final int PORT = 327
70
;
private
static
final
String
KEYSPACE
=
"Kiekpad"
;
//
private static final String KEYSPACE = "Kiekpad";
private
Session
session
;
// TODO final
private
final
static
int
WAITING_SLEEP_MILLIS
=
1000
;
public
CassandraService
()
{
private
Session
session
;
try
{
final
Cluster
cluster
=
Cluster
.
builder
().
addContactPoint
(
IP_ADDRESS
).
withPort
(
PORT
).
build
();
@Autowired
this
.
session
=
cluster
.
connect
(
KEYSPACE
);
public
CassandraService
(
@Value
(
"${cassandra.address}"
)
final
String
host
,
@Value
(
"${cassandra.port}"
)
final
int
port
,
}
catch
(
NoHostAvailableException
|
InvalidQueryException
exception
)
{
@Value
(
"${cassandra.keyspace}"
)
final
String
keyspace
,
@Value
(
"${cassandra.timeout}"
)
final
int
timeoutInMillis
)
{
this
.
session
=
null
;
createSession
(
host
,
port
,
keyspace
,
timeoutInMillis
);
}
}
}
public
Session
getSession
()
{
public
Session
getSession
()
{
return
session
;
return
session
;
}
}
private
void
createSession
(
final
String
host
,
final
int
port
,
final
String
keyspace
,
final
int
timeoutInMillis
)
{
final
Instant
start
=
Instant
.
now
();
Cluster
cluster
=
Cluster
.
builder
().
addContactPoint
(
host
).
withPort
(
port
).
build
();
while
(
true
)
{
try
{
this
.
session
=
cluster
.
connect
(
keyspace
);
break
;
}
catch
(
NoHostAvailableException
exception
)
{
// Host not unavailable
System
.
out
.
println
(
"Waiting for host..."
);
// TODO
if
(
Duration
.
between
(
start
,
Instant
.
now
()).
toMillis
()
<
timeoutInMillis
)
{
cluster
.
close
();
cluster
=
Cluster
.
builder
().
addContactPoint
(
host
).
withPort
(
port
).
build
();
try
{
Thread
.
sleep
(
WAITING_SLEEP_MILLIS
);
}
catch
(
InterruptedException
e
)
{
throw
new
IllegalStateException
(
e
);
}
}
else
{
throw
exception
;
}
}
catch
(
InvalidQueryException
exception
)
{
// Keyspace does not exist
System
.
out
.
println
(
"Create Keyspace..."
);
// TODO
createKeyspaceIfNotExists
(
cluster
,
keyspace
);
}
}
}
private
void
createKeyspaceIfNotExists
(
final
Cluster
cluster
,
final
String
keyspace
)
{
Session
session
=
cluster
.
connect
();
session
.
execute
(
"CREATE KEYSPACE IF NOT EXISTS "
+
keyspace
+
" WITH replication "
+
"= {'class':'SimpleStrategy', 'replication_factor':1};"
);
session
.
close
();
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment