I am using this to consume events reserved to "telemetry-feed-1". I am using "io.quarkus.vertx.ConsumeEvent" with quarkus. I want to use regEx like "telemetry-feed-.*" to reserve any channel that start with "telemetry-feed-". any one know how I can do this. Thank you very much.
#ConsumeEvent("telemetry-feed-1" )
public void publishMatchingSubscriptions(String message) throws Exception {
// process data
}
The #ConsumeEvent annotation in Quarkus just makes the use of io.vertx.core.eventbus.EventBus easier and since the latter does not support regex, neither does the annotation
Related
I am working on a project in intellij using java and spring.
I want to change in multiple files my api's in a similar way like this:
instead of:
public void someApi(HttpServletRequest request, HttpServletResponse response) throws Exception {
someThrift thriftRequest = getThrift(...);
someOtherThrift thriftResponse = …
setThriftResponse(...);
}
use this:
#ThriftResponse
public someThrift getReports(#ThriftRequestBody someThrift thriftRequest) throws Exception {
someOtherThrift thriftResponse = …
return thriftResponse;
}
is there a way to achieve this using some sort of a macro?
this kind of code spans on multiple files that all have the same suffix in their name as well
thank you
As said in the comments, you can use Structural search and replace. It allows you to search and replace fragment of codes using a template defined with variables constraint by count numbers, regular expressions, and even Groovy scripts.
The easiest way to create a template is to browse the list of existing ones, find one similar to what you want to achieve and modify it.
ArgumentMatchers.matches( String regex ) exists... and it is possible to devise regexes which don't match a given String. But it is far from trivial (several threads in SO).
Is it wrong of me (or wrong-headed) to think it might be a nice idea to request the Mockito designers to take the heavy-lifting out of this and add it as a feature? It just seems that, in the context of mocking and so forth, it is a far-from-exceptional use case...
PS also, I'm not clear with ArgumentMatchers.matches how you go about saying "this may be a multiline String we're matching against, don't worry about it"... wouldn't it be better to have a Pattern rather than a simple String?
later
Feature request "enhanced" at Mockito HQ (on Github). "bric3" there says one should use Jeff Bowman's technique for "does not match". But she/he seems to think the Pattern idea is worth thinking about.
Re not(): Mockito's own documentation says "Use additional matchers very judiciously because they may impact readability of a test. It is recommended to use matchers from Matchers and keep stubbing and verification simple."
Also I find I must "possible dupe" my own question: How to write a matcher that is not equal to something. Searching with hindsight is always easier...!
later still
Many thanks to Brice for adding this so quickly. Updated my gradle.build and... new 4.1 core downloaded from Maven Central and immediately available for use.
No need for a request: You can compose what you want using AdditionalMatchers.not.
when(yourComponent.acceptString(not(matches("foo|ba[rz]"))))
.thenThrow(new IllegalArgumentException());
If you want to match a Pattern, you might need to write your own ArgumentMatcher subclass, but it's quite easy from there:
public class MatchesPattern implements ArgumentMatcher<String> {
private final Pattern pattern;
public MatchesPattern(Pattern pattern) { this.pattern = pattern; }
#Override public boolean matches(String string) {
return pattern.matcher(string).matches();
}
#Override public String toString() {
return "[string matching /" + pattern.toString() + "/]";
}
/** Optional. */
public static MatchesPattern matchesPattern(Pattern pattern) {
return new MatchesPattern(pattern);
}
}
You can then consume that class using:
when(yourComponent.acceptString(not(argThat(new MatchesPattern(yourPattern)))
.thenThrow(new IllegalArgumentException());
// or with the static factory method:
when(yourComponent.acceptString(not(argThat(matchesPattern(yourPattern)))
.thenThrow(new IllegalArgumentException());
For future readers, Mockito 2.4.1 has been released with support of the Pattern class :
Now you should be abble to write :
when(yourComponent.acceptString(not(matches(Pattern.compile(...)))
.thenThrow(new IllegalArgumentException());
I want to set a property on a Camel Exchange and then use this property when saving the file. In my camel dsl I have the following:
.process(processorToSetExhangeProperty) // sets the property <uid> on the exchange
.to("file:/tmp?fileName=file-" + property("uid") + ".xml")
The file is being saved as:
"file-property{uid}.xml" though
My processor is as follows:
#Override
public void process(Exchange exchange) throws Exception {
UUID uuid = UUID.randomUUID();
exchange.setProperty("uid", uuid.toString());
exchange.setOut(exchange.getIn());
}
Any thoughts on what may be going wrong or how I can achieve this?
The to in the Camel is not interpreted at runtime.
You should use recipientList if you want to construct your URI dynamically.
See https://camel.apache.org/manual/latest/faq/how-to-use-a-dynamic-uri-in-to.html
UPDATED
New answer accepted above instead of this previous one:
The answer is [was]:
.to("file:/tmp?fileName=file-${property.uid}") + ".xml")
This simple expression pulls in the exchange property. For a complete list of what you can pull in, see the Simple Expression Language Reference
Please use toD() if you want a dynamic destination. The expression in the parenthesis is interpreted with simple() language. No simple() needed.
toD("file:/tmp?fileName=file-${exchangeProperty.uid}.xml")
But pay attention to not creating too many endpoints.
https://camel.apache.org/components/3.18.x/eips/toD-eip.html
I want to know the best method to schedule a code. I have a code that generates reports and sends mail to a set of people at interval of 24hrs. Its a console based java application. I want to know the best method to schedule that. Sometimes I may need to change that to 12hrs interval. However the application doesn't perform any other task in between the interval.
Here are few approach, from simplest to most comprehensive:
sleep():
TimeUnit.HOURS.sleep(24)
This approach is very simple, do the work and sleep for 24 hours. Actually it is a bit more complex because the report generation takes some time, so you have to sleep slightly shorter. All solutions below handle this transparently.
java.util.Timer#scheduleAtFixedRate() - simple, built-in Java solution.
#Scheduled annotation in spring or #Schedule in ejb - more complex but also more powerful, e.g. accepts cron expressions:
#Scheduled(fixedRate=DateUtils.MILLIS_PER_DAY)
public void generateReport() {
//...
}
quartz-scheduler - full blown Java scheduler with clustering and fail-over, misfire handling, full cron support, etc. Very comprehensive:
newTrigger().
withSchedule(
simpleSchedule().
withIntervalInHours(24).
repeatForever()
).build();
or
newTrigger().
withSchedule(
cronSchedule().
dailyAtHourAndMinute(17, 30). //17:30
).build();
I am using two ways:
First for non managed code like client code:
Chron4J
Second is implmented in the JavaEE framewoks. You can use it via annotating methods when you use an container like Glassfish/JBoss. Would be something like this:
#Schedule(second="*/1", minute="*",hour="*", persistent=false)
public void doWork(){
System.out.println("timer: " + helloService.sayHello());
}
I would take a look at the quartz scheduler if I were you.
I used in a number of applications and it's really easy to use.
You can find more information here: http://quartz-scheduler.org/
If you use the spring stack I would surely recommend it, since it is super easy to configure in xml and let spring inject all the stuff for you.
Well, if the program can be idle try something like this
try
{
for (;;) {
//code
Thread.sleep(1000 * 60 * 60 * 24);
//code
}
}
catch(Exception e)
{
System.out.println(e);
}
I would like to allow access to a particular method to more than one group of users. Is it possible in Spring Security 3.x to do such a thing using the #Secured annotation? Consider two groups (roles) OPERATOR and USER, would this code be valid:
#Secured("ROLE_OPERATOR", "ROLE_USER")
public void doWork() {
// do useful processing
}
You're almost there. Syntactically, you need to write it like this:
#Secured({"ROLE_OPERATOR", "ROLE_USER"})
public void doWork() { ... }
This is because you're supplying multiple values to a single array attribute of the annotation. (Java syntactically special-cases handing in a single value, but now you need to do it “properly”.)
#Donal Fellows answer is correct for Spring apps. However, if you're working in Grails, you need to use the Groovy syntax for lists so the code would look like this
#Secured(["ROLE_OPERATOR", "ROLE_USER"])
public void doWork() { ... }